How do I run a batch file from my Java Application?

后端 未结 11 1695
情书的邮戳
情书的邮戳 2020-11-22 00:42

In my Java application, I want to run a batch file that calls \"scons -Q implicit-deps-changed build\\file_load_type export\\file_load_type\"

It seems t

11条回答
  •  悲哀的现实
    2020-11-22 01:25

    Sometimes the thread execution process time is higher than JVM thread waiting process time, it use to happen when the process you're invoking takes some time to be processed, use the waitFor() command as follows:

    try{    
        Process p = Runtime.getRuntime().exec("file location here, don't forget using / instead of \\ to make it interoperable");
        p.waitFor();
    
    }catch( IOException ex ){
        //Validate the case the file can't be accesed (not enought permissions)
    
    }catch( InterruptedException ex ){
        //Validate the case the process is being stopped by some external situation     
    
    }
    

    This way the JVM will stop until the process you're invoking is done before it continue with the thread execution stack.

提交回复
热议问题