Runtime.exec()執行linux shell

來源:互聯網
上載者:User

 

最好的執行系統命令的方法就是寫個bat檔案或是shell指令碼。然後調用,那樣修改和實現就簡點多了。

 

現在執行外部命令,主要的方式,還是通過調用所以平台的SHELL去完成,WINDOWS下面就用CMD,LINUX或者是UNIX下面就用SHELL,下面示範一個LINUX下面用SHELL的調用,並把結果回顯到控制台上,其它的應用程式類。 
import java.io.BufferedReader;<br />import java.io.IOException;<br />import java.io.InputStream;<br />import java.io.InputStreamReader;<br />public class RuntimeTest {<br />/**<br /> * @param args<br /> */<br />public static void main(String[] args) {<br />// TODO Auto-generated method stub<br />Process process;<br />//String cmd = "ifconfig";//ok<br />//String cmd = "sar -u 1 1| awk 'NR==4 {print $8}'";//空白。管道不支援<br />String cmd = "/home/heyutao/workspace/ChunkOperator/sh/cpu.sh";//ok</p><p>try {<br />// 使用Runtime來執行command,產生Process對象<br />Runtime runtime = Runtime.getRuntime();<br />process = runtime.exec(cmd);<br />// 取得命令結果的輸出資料流<br />InputStream is = process.getInputStream();<br />// 用一個讀輸出資料流類去讀<br />InputStreamReader isr = new InputStreamReader(is);<br />// 用緩衝器讀行<br />BufferedReader br = new BufferedReader(isr);<br />String line = null;<br />while ((line = br.readLine()) != null) {<br />System.out.println(line);<br />}<br />is.close();<br />isr.close();<br />br.close();<br />} catch (IOException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />}<br />}<br />}<br /> 

 

cpu.sh的內容:

 

#!/bin/sh<br />#top<br />cpu=$(sar -u 1 1| awk 'NR==4 {print $8}')<br />ip=$(ifconfig | grep -E 'inet addr|inet 地址'| grep -v '127.0.0.1' | awk -F ':' '{print $2}' | awk '{print $1}')<br />echo "cpu:$ip:$cpu" 

 

執行結果如下: 

 

cpu:59.64.158.126:80.68

Sun的doc裡其實說明還有其他的用法:
exec(String[] cmdarray, String[] envp, File dir)
Executes the specified command and arguments in a separate process with the specified environment and working directory.

那個dir就是調用的程式的工作目錄,這句其實還是很有用的。

Windows下調用程式

Process proc =Runtime.getRuntime().exec("exefile");

Linux下調用程式就要改成下面的格式

Process proc =Runtime.getRuntime().exec("./exefile");

Windows下調用系統命令

String [] cmd={"cmd","/C","copy exe1 exe2"};
Process proc =Runtime.getRuntime().exec(cmd);

Linux下調用系統命令就要改成下面的格式

String [] cmd={"/bin/sh","-c","ln -s exe1 exe2"};
Process proc =Runtime.getRuntime().exec(cmd);

Windows下調用系統命令並彈出命令列視窗

String [] cmd={"cmd","/C","start copy exe1 exe2"};
Process proc =Runtime.getRuntime().exec(cmd);

Linux下調用系統命令並彈出終端視窗就要改成下面的格式

String [] cmd={"/bin/sh","-c","xterm -e ln -s exe1 exe2"};
Process proc =Runtime.getRuntime().exec(cmd);

還有要設定調用程式的工作目錄就要

Process proc =Runtime.getRuntime().exec("exeflie",null, new File("workpath"));

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.