java调用exe,传入参数、获取返回值

参考博文http://blog.sina.com.cn/s/blog_4b2fd9350100qd3n.html

final Runtime runtime = Runtime.getRuntime();
Process process = null;
String[] cmds = {"E:\\222\\FFmpeg.exe","E:\\huayupart.mp4"};
try {
    process = new ProcessBuilder(cmds).start();
    System.out.println(process.isAlive())            //true
    int exitVal = process.waitFor();
    System.out.println(exitVal);                //0
    System.out.println(process.isAlive())            //false
    }
 catch(Exception e) {
       System.out.println(e.getMessage)
    }

因为我调用的exe中必填参数(可能是这个原因),所以如果cmds[1]如果为null,会报nullpointerExeception;

isAlive()判断进程是否存活;waitfor()会阻塞等待进程执行完,如果调用这个方法则会直接往下走。

 

还有其他调用exe的方法

//把C盘信息写到aa.txt中
Process pro = Runtime.getRuntime().exec("cmd.exe /c dir c: > d://aa.txt");

//打开文本11.txt
Process pro = Runtime.getRuntime().exec("rundll32 url.dll FileProtocolHandler file://E:\11.txt");

process = runtime.exec("cmd.exe /c start E:\\111\\FFmpeg.exe file://E:\\hongtu.mp4")

 

你可能感兴趣的:(java调用exe,传入参数、获取返回值)