简体   繁体   English

Java Runtime.exec运行Java问题

[英]Java Runtime.exec running java problem

I have a simple server application, which I would like to run in the background. 我有一个简单的服务器应用程序,我想在后台运行。 The following line works for me: 以下行对我有用:

Runtime.getRuntime().exec("cmd /c start java -jar ..\\server\\server.jar -Dlog4j.configuration=file:src\\test\\resources\\log4j.properties -filename src\\test\\resources\\server.properties");

But it displays the cmd window and I am unable to destroy it. 但是它显示了cmd窗口,我无法销毁它。 So I would like to use 所以我想用

Runtime.getRuntime().exec("java -jar ..\\server\\server.jar -Dlog4j.configuration=file:src\\test\\resources\\log4j.properties -filename src\\test\\resources\\scIntegration.properties");

But it simply doesn't connect to the server. 但是它根本不连接到服务器。 So why is that? 那为什么呢?

A related question. 一个相关的问题。 How do I end the process? 如何结束流程? It is a server that "doesn't end". 它是“永不止息”的服务器。 So I have to kill it and I would assume, that running the java only command would be capable to be destroyed, but with the cmd I have no luck there. 因此,我必须杀死它,并且我认为,运行仅java命令将能够被销毁,但是使用cmd我没有运气了。

您应该将命令拆分为一个数组,其中第一个参数是要运行的实际命令,其余所有都是类似参数的命令:

Runtime.getRuntime().exec(new String[] {"/usr/bin/java", "-jar", "..\\server\\server.jar" ...});

Try using an absolute path to the java programm. 尝试使用Java程序的绝对路径。

For destroying: exec() returns a java.lang.Process, which you should be able to destroy. 对于销毁: exec()返回一个java.lang.Process,您应该可以销毁它。 If not, you have to implement some type of callback to shut your server down, eg listening on a specific prot for a shutdown command. 如果不是,则必须实现某种类型的回调以关闭服务器,例如,侦听特定prot上的shutdown命令。

The server is outputing something to stdout and in the shortened command version it didn't have a place to output, so it got stuck while trying to output some data. 服务器正在向stdout输出某些内容,在缩短的命令版本中,它没有输出位置,因此在尝试输出一些数据时卡住了。 The solution is to pipe the stdout to eg some file. 解决方案是将标准输出通过管道传输到某些文件。

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

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