Java执行cmd命令
1.Java执行cmd
public void executeCMD() { try { Runtime mt = Runtime.getRuntime(); String cmd = "ping 127.0.0.1"; Process pro = mt.exec(cmd); InputStream ers= pro.getErrorStream(); pro.waitFor(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
2.Java执行Bat
1.绝对路径
// An highlighted block public void executeBat() { try { Runtime run = Runtime.getRuntime(); String cmd = "E:\\Re-Architecture\\test.bat" Process pro = run.exec(cmd); InputStream ers= pro.getErrorStream(); pro.waitFor(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
2.要跨盘符
// An highlighted block public void executeCMD() { try { Runtime run = Runtime.getRuntime(); Process pro =run.exec("cest.bat",null,new File("E:\\Re-Architecture\\")); InputStream ers= pro.getErrorStream(); pro.waitFor(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
3.Java执行exe
// An highlighted block public void executeExe() { try { Runtime run = Runtime.getRuntime(); Process pro =run.exec("cmd /c E:\\Re-Architecture\\test.exe")); InputStream ers= pro.getErrorStream(); pro.waitFor(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
4.Java执行快捷图标
// An highlighted block public void executeBat() { try { Runtime mt = Runtime.getRuntime(); String cmd = "rundll32 SHELL32.DLL,ShellExec_RunDLL "+"E:\\ImportMetapediaCMD\\noUAC.CreateMetapedia.lnk"; Process pro = mt.exec(cmd); InputStream ers= pro.getErrorStream(); pro.waitFor(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
5.查看log
// An highlighted block public class RuntimeTest { public static void main(String[] args) { RuntimeTest s = new RuntimeTest(); s.test(); } public void test(){ Runtime run =Runtime.getRuntime(); try { Process p = run.exec("ping 127.0.0.1"); InputStream ins= p.getInputStream(); InputStream ers= p.getErrorStream(); new Thread(new inputStreamThread(ins)).start(); p.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } class inputStreamThread implements Runnable{ private InputStream ins = null; private BufferedReader bfr = null; public inputStreamThread(InputStream ins){ this.ins = ins; this.bfr = new BufferedReader(new InputStreamReader(ins)); } @Override public void run() { String line = null; byte[] b = new byte[100]; int num = 0; try { while((num=ins.read(b))!=-1){ System.out.println(new String(b,"gb2312")); } } catch (IOException e) { e.printStackTrace(); } } } }
6.报警
1.java.io.IOException: Cannot run program “****”: CreateProcess error=2
// 语法错误,即命令不能被正确识别 run.exec("%comspec% /c \ceshi\ceshi.bat"); //正确语法 run.exec(new String[]{"cmd","/c","%comspec% /c \ceshi\ceshi.bat"},null,null);
2.java.lang.IllegalArgumentException: Executable name has embedded quote,split the arguments
// 可能是命令中有空格识别异常,可以试着把空格转成带引号的空格 run.exec("c:\\ceshi file\\ceshi.bat".replace(" ","" ""))
3.bat命令在CMD窗口中可以正常执行,但通过java的Runtime执行就会阻塞,八成可能是命令执行中开了新窗口,但老窗口没关导致的
// An highlighted block run.exec(new String[]{"cmd","/k","ceshi.bat"},null,new File("d:\\ceshi\\")); 要把“/k”换成“/c” run.exec(new String[]{"cmd","/c","ceshi.bat"},null,new File("d:\\ceshi\\"));