How can Java execute batch file in separate process tree

試著忘記壹切 提交于 2019-12-24 00:44:15

问题


I'm writing my Java app a update mechnism. I create a windows batch file,exit my program and the batch file continues to delete my Jar, copy the new one from a remote location, start the jar. My problem: deletion + copy works, BUT - the application won't start. I think the problem is that I don't know how to make Java to execute batch file in separate process tree. when running this: Runtime.getRuntime() I open a child process.

So my question - How can Java execute batch file in separate process tree?


回答1:


Here is a workaround that uses cmd as the interminent layer:

public class Main {
    public static void main(String[] args) throws Exception {
        Process p = Runtime.getRuntime().exec("cmd /c c:\\test.bat");
    }
}

where test.bat will contain

@echo off
PING 1.1.1.1 -n 1 -w 5000 >nul
java -jar "[path]"



回答2:


I believe the answer lies buried in the link that Jakub Zaverka provided. Use start instead of cmd to start the batch file. This will give the batch its own console window.




回答3:


I solved the problem. It was due to launch4j - the program i warped my JAR with. Because it allowed one instance, even when I deleted the pre-upgrade file, it didn't allow me to start the new instance from the command line that started under the pre-upgrade program. Now I warp my program without the one instance validation and it works fine! Thanks everybody for their kind help!

BTW - I validate one instance with this solution - How to allow running only one instance of a Java program at a time?



来源:https://stackoverflow.com/questions/9657038/how-can-java-execute-batch-file-in-separate-process-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!