开发者

Executing batch from Java

开发者 https://www.devze.com 2023-01-12 00:53 出处:网络
Let\'s say, I have a Java class JavaClass1.java.I am executing 2 batch files from开发者_JAVA百科 this Java class.

Let's say,

I have a Java class JavaClass1.java.I am executing 2 batch files from开发者_JAVA百科 this Java class. Each .bat files starts another Java application. Lets say these other two app takes 15 hour each to complete, and they are also not dependent to each other.

How can I solve this issue. I dont have to wait for one to complete so that I have to start another, I can do it simultaneously also.

I found people talking about handelling outputstream, inputstream and error stream, if I wait for the errors to handle, then I have to wait 15 hours for each. I dont want to do that.

Is there any way? Please suggest. Thanks


Place the mechanism for launching each .bat in its own thread, then start each thread.

    new Thread(new Runnable() {
        @Override
        public void run() {
            //Launch first bat.
        }
    }).start();

    new Thread(new Runnable() {
        @Override
        public void run() {
            //Launch second bat.
        }
    }).start();


Runtime.getRuntime().exec(new String[]{"cmd","/c","java -jar app1.jar"});
Runtime.getRuntime().exec(new String[]{"cmd","/c","java -jar app2.jar"});

Just use Runtime execute service, if you don't call process.waitFor() to get return code of process, it won't blocking, so you can call next app immediately. If you want return code from app, run app on each Thread as Mike.

0

精彩评论

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