简体   繁体   English

不关闭Java应用程序就无法从Java启动外部程序

[英]Can't launch external program from Java without closing java app

I'm trying to launch an external program from my java swing app using this: 我正在尝试使用以下命令从我的Java swing应用程序启动外部程序:

Process proc = Runtime.getRuntime().exec(cmd);

But the external program never actually gets launched until I close out of my java app...everytime. 但是,直到我关闭Java应用程序外,外部程序才真正启动。 It waits to launch only after I have closed out. 只有在我关闭后,它才会等待启动。

the external program I am trying to run is an exe that takes arguments so: 我尝试运行的外部程序是一个带有参数的exe,因此:

cmd = "externalProgram.exe -v --fullscreen --nowing";

What could possibly be wrong here. 这里可能出了什么问题。 Funny enough it works as expected if i try something simple like: 有趣的是,如果我尝试简单的操作,它可以按预期工作:

Process proc = Runtime.getRuntime().exec("notepad.exe");

You may need to read from the process's standard output, or close the standard input, before it will proceed. 在继续进行之前,您可能需要从流程的标准输出中读取或关闭标准输入。 For reading the output, the problem is that the buffer can get full, blocking the program; 为了读取输出,问题在于缓冲区可能已满,从而阻塞了程序; for closing the input, the problem is that some programs will try to read data from there if it's available, waiting to do so. 对于关闭输入,问题在于某些程序将尝试从那里读取数据(如果有),等待这样做。 One or both of these tricks is very likely to straighten things out for you. 这些技巧中的一个或两个很可能会为您解决问题。

You may also read the error output stream to check it the program is actually being unsuccessfully executed 您还可以阅读错误输出流,以检查程序实际上是否未成功执行

String cmd = "svn.exe";
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String line = null;
while((line=reader.readLine())!=null){
   System.out.println(line);
}
reader.close();

My console shows 我的控制台显示

Type 'svn help' for usage. 输入“ svn help”以供使用。

Which evidently shows the program was executed by Java. 这显然表明该程序是由Java执行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM