开发者

Exit after initializing batch script from java

开发者 https://www.devze.com 2023-03-05 02:35 出处:网络
I want to initialize one batch script using java code. Once it is initialized I need to exit the java program and wants the batch script to continue executing. How can I achieve this. I used Runtime a

I want to initialize one batch script using java code. Once it is initialized I need to exit the java program and wants the batch script to continue executing. How can I achieve this. I used Runtime and Process to do this. Bu开发者_如何学编程t it is waiting for the batch script to finish before proceeding to the next. This is the sample program I tried out.

        try {
            Runtime r = Runtime.getRuntime();
            System.out.println("Executing process");
            Process p = r.exec("c:\\anoop\\ping.bat");
            System.out.println("Executed process"); 
            p.waitFor();
            System.out.println("Exiting with out :: ");
        } catch (Exception e) {// Catch exception if any
            System.err.println("Error: " + e.getMessage());
            e.printStackTrace();
        }

If I use p.exitValue(), it is giving error java.lang.IllegalThreadStateException: process has not exited. In short, I just want to initialize a batch script and then exit from the java program.

Thanks, Anoop


To get the exit value of the process, use:

int res = p.waitFor();
System.out.println("Exiting with out :: " + res);

If you want the process to just continue, don't wait for it (don't call wait())

If the process expects input, or produces some output, you have to handle that (preferably on separate threads) since all I/O is redirected.

0

精彩评论

暂无评论...
验证码 换一张
取 消