简体   繁体   中英

How to run windows console application in java code using exec() method?

how use Runtime.getRuntime().exec()?

I try:

try {
    String[] callAndArgs = {"path\\program.exe","arg1","arg2","arg3"}
    System.out.println("before");
    Process p = Runtime.getRuntime().exec(callAndArgs)
    System.out.println("in");
    p.waitFor();
    System.out.println("after");
} catch (IOException e) {
            System.out.println("catch io: " + e.getMessage());
        } catch (InterruptedException ie) {
            System.out.println("catch ie: " + ie.getMessage());
        }

And program doesn't work. Output:

before
in
after

hm? help;p

Maybe this could help too ( http://ubuntuforums.org/showthread.php?t=681779 ),

import java.lang.* ;

 public class MyJavaClass
 {
 public void runCmd(String[] args)
 {
 String cmd = "/home_dir/./my_shell_script.sh" ;
 Runtime run = Runtime.getRuntime() ;
 Process pr = run.exec(cmd) ;
 pr.waitFor() ;
 BufferedReader buf = new BufferedReader( new InputStreamReader( pr.getInputStream() ) ) ;

 while ( ( String line ; line = buf.readLine() ) != null ) 
 {
 System.out.println(line) ;
 }

 }

 }

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