如何从另一个Java应用程序打开Java桌面工具
我使用nebeans平台模块创建了一个Java桌面工具。 现在,我们正在创建一个通用工具,单击一个按钮就可以启动现有工具 假设我们有一个JFrame,其中有两个按钮。 一个是tool1_btn 第二个是tool2_btn 现在,当我单击工具1时,应该会弹出一个工具 如果我写得像如何从另一个Java应用程序打开Java桌面工具,java,platform,Java,Platform,我使用nebeans平台模块创建了一个Java桌面工具。 现在,我们正在创建一个通用工具,单击一个按钮就可以启动现有工具 假设我们有一个JFrame,其中有两个按钮。 一个是tool1_btn 第二个是tool2_btn 现在,当我单击工具1时,应该会弹出一个工具 如果我写得像 try { String line; Process p = Runtime.getRuntime().exec("notepad.exe"); /* java -classpath C:\\
try {
String line;
Process p = Runtime.getRuntime().exec("notepad.exe");
/* java -classpath C:\\projects\\workspace\\testing\\bin test.TestOutput"
* Create a buffered reader from the Process input stream.
*/
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
/**
* Loop through the input stream to print the program output into console.
*/
while ((line = input.readLine()) != null) {
System.out.println(line);
}
/**
* Finally close the reader
*/
input.close();
} catch (Exception e) {
e.printStackTrace();
}
将弹出notepad.exe
但是,如果我们为我现有的工具jar提供一个Java类路径,它只会在后端运行它
我想打开这个工具,就像双击并打开一样。有可能吗?请帮我
如果我的问题不清楚,请通知我。我这样做:
File exe=新文件(…);
Runtime.getRuntime().exec(“rundll32 url.dll,FileProtocolHandler”+exe.getAbsolutePath()) 您应该首先在普通的
流程上使用ProcessBuilder
。除此之外,它还允许您重定向错误流(到输出流和其他位置),以及指定程序的启动上下文(程序将在哪个目录中启动)
类似于
try {
String line;
ProcessBuilder pb = new ProcessBuilder(
"java",
"-cp",
"C:\\projects\\workspace\\testing\\bin",
"test.TestOutput"
);
pb.redirectError();
Process p = pb.start();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
/**
* Loop through the input stream to print the program output into console.
*/
while ((line = input.readLine()) != null) {
System.out.println(line);
}
/**
* Finally close the reader
*/
input.close();
} catch (Exception e) {
e.printStackTrace();
}
例如