Java執行window命令和linux命令
阿新 • • 發佈:2018-12-20
這裡強調一點,命令中用到的jdk還有其他一些需要配置的軟體的環境變數一定要配置成全域性的,外部可以讀取的,否則java根本無法執行命令。比如linux需要配置在 /etc/profile,用source /etc/profile可以即時生效。
下面是程式碼:
package com.apk.openUser.utils; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; public class AddLinuxUtil { //命令之間都是用 && 進行連線,如果命令多,可以寫成可執行的指令碼。 //linux 命令的執行,方式1 public static List<String> executeNewFlow(List<String> commands) { List<String> rspList = new ArrayList<String>(); Runtime run = Runtime.getRuntime(); try { Process proc = run.exec("/bin/bash", null, null); BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true); for (String line : commands) { out.println(line); } // out.println("cd /home/test"); // out.println("pwd"); // out.println("rm -fr /home/proxy.log"); out.println("exit");// 這個命令必須執行,否則in流不結束。 String rspLine = ""; while ((rspLine = in.readLine()) != null) { System.out.println(rspLine); rspList.add(rspLine); if (rspLine.equals("BUILD SUCCESSFUL")) { System.out.println("成功"); } } proc.waitFor(); in.close(); out.close(); proc.destroy(); } catch (IOException e1) { e1.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return rspList; } //方式二: public static int executeNewFlow(String bf,String af,String app) { //System.out.println(commands); Runtime run = Runtime.getRuntime(); int iden = 0; //String[] cmds = {"/bin/sh", "-c", commands}; String cmds = "cp "+bf+" -vf "+af+" && "+app; BufferedReader in = null; //BufferedReader er = null;//輸出執行錯誤的命令內容 try { Process proc = run.exec(new String[]{"/bin/sh","-c",cmds}); //Process proc = run.exec(cmds); in = new BufferedReader(new InputStreamReader(proc.getInputStream())); //er = new BufferedReader(new InputStreamReader(proc.getErrorStream())); String rspLine = ""; StringBuffer b=new StringBuffer(); while ((rspLine = in.readLine()) != null) { System.out.println(rspLine); b.append(rspLine+"\n"); if (rspLine.indexOf("SUCCESSFUL") != -1) { System.out.println("成功"); iden = 1; } } String rline = null; System.out.println(b.toString()); /*while ((rline=er.readLine())!=null) { System.out.println("error::::"+rline); }*/ proc.waitFor(); } catch (Exception e1) { e1.printStackTrace(); }finally { if (in != null) { try { in.close(); } catch (Exception e) { e.printStackTrace(); } } } return iden; } //windows命令的執行 public static int exeCmd(String cmd,String apath,String bpath) { Runtime runtime = Runtime.getRuntime(); BufferedReader br = null; int iden = 0;//未打包成功 String cmd1 = "cmd /c replace "+apath+" "+bpath+" /s && "+cmd; System.out.println("cmd:"+cmd1); try { Process pro = runtime.exec(cmd1); br = new BufferedReader(new InputStreamReader(pro.getInputStream(),"gbk")); //StringBuffer b = new StringBuffer(); String line=null; StringBuffer b=new StringBuffer(); while ((line=br.readLine())!=null) { b.append(line+"\n"); System.out.println(line); if (line.indexOf("SUCCESSFUL") != -1) { System.out.println("成功"); iden = 1; } } System.out.println(b.toString()); pro.waitFor(); } catch (Exception e) { e.printStackTrace(); }finally { if (br != null) { try { br.close(); } catch (Exception e) { e.printStackTrace(); } } } return iden; } public static void main(String[] args) { /*String[] it = {"D:","cd androidwork\\weptt-text","gradle build"};//&& cd androidwork\\weptt-text && gradle build exec(it);*/ // String cmd = "java -version";// D:\\androidwork\\weptt-text && gradle build"; // // String mCurrentPath = System.getProperty("user.dir"); // String mCurrentDrive = mCurrentPath.substring(0, 2); String cmdCode = "cd D:\\androidwork\\weptt && gradle clean build "; String apath = "D:\\userCustom\\81111\\ptt_config.xml"; String bpath = "D:\\androidwork\\weptt\\WepttApp\\src\\main\\res\\menu"; System.out.println("列印Code: >>> "+cmdCode); int m = exeCmd(cmdCode,apath,bpath); System.out.println("mm:"+m); //TimingSchedule tl = new TimingSchedule(); //tl.temp1(); } }