将字符串作为参数从一个 Java 应用程序传递到另一个 Java 应用程序
我正在尝试将字符串作为参数从一个 Java 应用程序传递到第二个作为 StartUp 参数,
例如,我有一些应用程序必须在 System.exit(0 之前调用启动另一个 Java 应用程序(仅包含 JOptionPane、JDialog 或简单的 JFrame) );
,我试图将一些描述从关闭的应用程序发送到另一个应用程序,
这些代码是我尝试过的模拟,在这种形式下,代码可以正常工作并将字符串显示到 JTextArea 中...
import java.io.IOException;
import java.util.concurrent.*;
public class TestScheduler {
public static void main(String[] args) throws InterruptedException {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(true);
for (int i = 0; i < 10; i++) {
final int j = i;
System.out.println("assign : " + i);
ScheduledFuture<?> future = executor.schedule(new Runnable() {
@Override
public void run() {
System.out.println("run : " + j);
}
}, 2, TimeUnit.SECONDS);
}
System.out.println("executor.shutdown() ....");
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
try {
Process p = Runtime.getRuntime().exec("cmd /c start java -jar C:\\Dialog.jar 'Passed info'");
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("System.exit(0) .....");
System.exit(0);
}
private TestScheduler() {
}
}
//
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
public class Main {
private static ArrayList<String> list = new ArrayList<String>();
public Main() {
JFrame frm = new JFrame();
JTextArea text = new JTextArea();
if (list.size() > 0) {
for (int i = 0; i < list.size(); ++i) {
text.append(list.get(i));
}
}
JScrollPane scroll = new JScrollPane(text,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.add(scroll, BorderLayout.CENTER);
frm.setLocation(150, 100);
frm.setSize(new Dimension(400, 300));
frm.setVisible(true);
}
public static void main(String[] args) {
if (args.length > 0) {
for (String s : args) {
list.add(s);
System.out.print(s + " ");
}
}
Main m = new Main();
}
}
我的问题:
编辑1:如果存在另一种方法如何将某些值从一个Java应用程序(必须称为System.exit(0);)传递到另一个Java应用程序,这是我尝试使用Process/ProcessBuilder
EDIT2的另一种方法:我的crosspost http://forums.oracle.com/forums/thread.jspa?threadID= 2229798&tstart=0
已接受 OTN 的答案
I'm trying to pass String as parameter from one Java Aplications to second as StartUp parameter
for example I have Aplications that must call start another Java Aplication (just contains only JOptionPane, JDialog or simple JFrame) before System.exit(0);
, there I trying to send some descriptions from closing application to another,
these code is simulations what I tried that and in this form, code works correctly and displayed String into the JTextArea ...
import java.io.IOException;
import java.util.concurrent.*;
public class TestScheduler {
public static void main(String[] args) throws InterruptedException {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(true);
for (int i = 0; i < 10; i++) {
final int j = i;
System.out.println("assign : " + i);
ScheduledFuture<?> future = executor.schedule(new Runnable() {
@Override
public void run() {
System.out.println("run : " + j);
}
}, 2, TimeUnit.SECONDS);
}
System.out.println("executor.shutdown() ....");
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
try {
Process p = Runtime.getRuntime().exec("cmd /c start java -jar C:\\Dialog.jar 'Passed info'");
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("System.exit(0) .....");
System.exit(0);
}
private TestScheduler() {
}
}
//
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
public class Main {
private static ArrayList<String> list = new ArrayList<String>();
public Main() {
JFrame frm = new JFrame();
JTextArea text = new JTextArea();
if (list.size() > 0) {
for (int i = 0; i < list.size(); ++i) {
text.append(list.get(i));
}
}
JScrollPane scroll = new JScrollPane(text,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.add(scroll, BorderLayout.CENTER);
frm.setLocation(150, 100);
frm.setSize(new Dimension(400, 300));
frm.setVisible(true);
}
public static void main(String[] args) {
if (args.length > 0) {
for (String s : args) {
list.add(s);
System.out.print(s + " ");
}
}
Main m = new Main();
}
}
my question :
EDIT1: if is there exist another way how to pass some value from one Java Aplication (there must be called System.exit(0);) to another Java Aplication, another way as I tried by using Process/ProcessBuilder
EDIT2: my crosspost http://forums.oracle.com/forums/thread.jspa?threadID=2229798&tstart=0
accepted answer from OTN
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jverd 在 OTN 上接受的答案
是的,还有其他方法。这种方式不能满足您的需求吗?
还有另一个采用数组的 exec() 签名,其中第一个元素是命令,其余元素是其参数。它可能是也可能不是可变参数调用。看起来像这样,尽管它可能并不完全像我所拥有的那样工作。
exec("cmd", "/c", "start", "java", "-jar", "C:\Dialog.jar", "Passed info");
// 或者
您可以将信息放入第二个进程读取的文件中。
您可以将信息存储在第二个进程查询的数据库中。
您可以让一个进程打开 ServerSocket,另一个进程连接到它并以这种方式发送数据。
您可以使用 JMS、Active MQ 等更高级的消息传递工具。
您可以使用 RMI。< /p>
您可以使用 CORBA。
我确信还有其他方法。
我不知道哪种方法最适合您的需求。这是您需要弄清楚的事情,尽管如果您无法决定,如果您在此处发布有关您的要求的更多详细信息,有人可能会提供一些建议。
accepted answer by jverd on OTN
Yes, there are other ways. Is this way not meeting your needs?
There's another exec() signature that takes an array, where the first element is the command and the rest of the elements are its args. It may or may not be a varargs call. That would look something like this, although it might not work exactly as I have it.
exec("cmd", "/c", "start", "java", "-jar", "C:\Dialog.jar", "Passed info");
// OR
You could put the information in a file that the second process reads.
You could store the information in a database that the second process queries.
You could have one process open a ServerSocket and the other connect to it and send the data that way.
You could use a higher-level messaging tool like JMS, Active MQ, etc.
You could use RMI.
You could use CORBA.
I'm sure there are other approaches as well.
I have no idea which approach is best suited to your needs. That's something you'll need to figure out, although if you can't decide, if you post more details about your requirements here, somebody may offer some advice.
伙计,
请阅读当运行时执行不会如果您仍然遇到困难,请联系我们。
这是一篇好文章。我猜您“参数有问题”;-)
干杯。基思.
Dude,
Read When runtime exec won't and get back to us if you're still stuck.
It's a good article. I'm guessing you've got "a problem with your parameter" ;-)
Cheers. Keith.