Java調用Linux命令(cd的處理)

來源:互聯網
上載者:User

標籤:time()   input   dwr   close   獨立   串連   tput   linux系統   style   

一、Java調用Linux系統的命令非常簡單

這是一個非常常用的調用方法樣本:

 1     public String executeLinuxCmd(String cmd) { 2         System.out.println("got cmd job : " + cmd); 3         Runtime run = Runtime.getRuntime(); 4         try { 5             Process process = run.exec(cmd); 6             InputStream in = process.getInputStream(); 7             BufferedReader bs = new BufferedReader(new InputStreamReader(in)); 8             // System.out.println("[check] now size \n"+bs.readLine()); 9             String result = null;10             while (in.read() != -1) {11                 result = bs.readLine();12                 System.out.println("job result [" + result + "]");13             }14             in.close();15             // process.waitFor();16             process.destroy();17             return result;18         } catch (IOException e) {19             e.printStackTrace();20         }21         return null;22     }

二、含有cd操作的方法樣本

1. 問題背景

1.1 java程式運行在/home/lings目錄下;

1.2 希望刪除/home/test目錄下的檔案proxy.log;

1.3 調用上面的介面兩次?

executeLinuxCmd("cd /home/test");executeLinuxCmd("rm -fr /home/proxy.log");

是不行的!

1.4 這個介面的調用是單次事務型的,就是每次調用都是獨立的事務或者說操作,沒有關聯的。

那這種“複雜”一點的操作流程怎麼辦呢?

1.5 方法a: 可以寫一個獨立的指令碼,然後一次運行指令碼,這樣多複雜的邏輯都沒問題。

1.6 方法b: 可以啟動一個shell長串連,保持串連,發送多條命令,最後釋放串連。

樣本邏輯代碼:

 1 public void executeNewFlow() { 2         Runtime run = Runtime.getRuntime(); 3         File wd = new File("/bin"); 4         System.out.println(wd); 5         Process proc = null; 6         try { 7             proc = run.exec("/bin/bash", null, wd); 8         } catch (IOException e) { 9             e.printStackTrace();10         }11         if (proc != null) {12             BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));13             PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);14             out.println("cd /home/test");15             out.println("pwd");16             out.println("rm -fr /home/proxy.log");17             out.println("exit");18             try {19                 String line;20                 while ((line = in.readLine()) != null) {21                     System.out.println(line);22                 }23                 proc.waitFor();24                 in.close();25                 out.close();26                 proc.destroy();27             } catch (Exception e) {28                 e.printStackTrace();29             }30         }31     }

 

Java調用Linux命令(cd的處理)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.