我正在尝试将String作为参数从一个Java Aplications传递到第二个作为StartUp参数
例如,我有必须在System.exit(0);
之前调用启动另一个Java Aplication(仅包含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();
}
}
我的问题:
EDIT1:如果存在另一种方法,如何将一个Java Aplication(必须称为System.exit(0);)传递给另一个Java Aplication,另一种方式是我尝试使用Process / ProcessBuilder
EDIT2:我的crosspost http://forums.oracle.com/forums/thread.jspa?threadID=2229798&tstart=0
接受OTN的答复
答案 0 :(得分:2)
jverd在OTN上接受了答案
是的,还有其他方法。这种方式不能满足您的需求吗?
还有另一个带有数组的exec()签名,其中第一个元素是命令,其余元素是args。它可能是也可能不是varargs。这看起来像这样,虽然它可能不会像我一样完全正常工作。
exec(“cmd”,“/ c”,“start”,“java”,“ - jar”,“C:\ Dialog.jar”,“传递信息”);
// OR
exec(new String[] {"cmd", "/c", "start", "java", "-jar", "C:\\Dialog.jar", "Passed info"});
您可以将信息放在第二个进程读取的文件中。
您可以将信息存储在第二个进程查询的数据库中。
您可以让一个进程打开一个ServerSocket,另一个进程连接到它并以这种方式发送数据。
您可以使用更高级别的消息传递工具,如JMS,Active MQ等。
您可以使用RMI。
您可以使用CORBA。
我确信还有其他方法。
我不知道哪种方法最适合您的需求。这是你需要弄清楚的,虽然如果你不能决定,如果你在这里发布有关你的要求的更多细节,有人可能会提供一些建议。
答案 1 :(得分:0)