简体   繁体   中英

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. 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.

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

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

Try using an absolute path to the java programm.

For destroying: exec() returns a java.lang.Process, which you should be able to destroy. 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.

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. The solution is to pipe the stdout to eg some file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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