日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

java代码运行linux shell操作

發(fā)布時(shí)間:2024/4/14 linux 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java代码运行linux shell操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.Java調(diào)用shell
??
Java語言以其跨平臺(tái)性和簡易性而著稱,在Java里面的lang包里(java.lang.Runtime)提供了一個(gè)允許Java程序與該程序所運(yùn)
行的環(huán)境交互的接口,這就是Runtime類,在Runtime類里提供了獲取當(dāng)前運(yùn)行環(huán)境的接口。
其中的exec函數(shù)返回一個(gè)執(zhí)行shell命令的子進(jìn)程。exec函數(shù)的具體實(shí)現(xiàn)形式有以下幾種:
public Process exec(String command) throws IOException
public Process exec(String command,String[] envp) throws
IOException
public Process exec(String command,String[] envp,File dir) throws
IOException
public Process exec(String[] cmdarray) throws IOException
public Process exec(String[] cmdarray, String[] envp) throws
IOException
public Process exec(String[] cmdarray, String[] envp,File dir)
throws IOException
??
我們?cè)谶@里主要用到的是第一個(gè)和第四個(gè)函數(shù),具體方法很簡單,就是在exec函數(shù)中傳遞一個(gè)代表命令的字符串。exec函數(shù)返回的是一個(gè)Process類
型的類的實(shí)例。Process類主要用來控制進(jìn)程,獲取進(jìn)程信息等作用。(具體信息及其用法請(qǐng)參看Java doc)。
?
1)執(zhí)行簡單的命令的方法:
代碼如下:
???????
try ???????????
String commands = "ls -l";
???????????
Process process = Runtime.getRuntime().exec (commands);
???????????
// for showing the info on screen
???????????
InputStreamReader ir=new
InputStreamReader(process.getInputStream());
???????????
BufferedReader input = new BufferedReader (ir);
???????????
String line;
???????????
while ((line = input.readLine ()) != null){
???????????????
System.out.println(line);
???????
}//end try
???????
catch (java.io.IOException e){
???????????
System.err.println ("IOException " + e.getMessage());
???????
} ? 上面的代碼首先是聲明了一個(gè)代表命令的字符串commands,它代表了ls -l
這個(gè)命令。之后我們用Runtime.getRuntime().exec(commands)來生成一個(gè)子進(jìn)程來執(zhí)行這個(gè)命令,如果這句話運(yùn)行成功,則
命令 ls -l 運(yùn)行成功(由于沒有讓它顯示,不會(huì)顯示ls -l
的結(jié)果)。后面的流操作則是獲取進(jìn)程的流信息,并把它們一行行輸出到屏幕。

2)執(zhí)行帶有參數(shù)的命令(尤其是參數(shù)需要用引號(hào)的)時(shí)則需要用String的數(shù)組來表示整個(gè)命令,而且要用轉(zhuǎn)義符把引號(hào)的特殊含義去除,例如我們要執(zhí)行
find / -name "*mysql*" -print 時(shí),用如下代碼
???????
try ???????????
String[] commands = new
String[]{"find",".","-name","*mysql*","-print"};
???????????
Process process = Runtime.getRuntime().exec (commands);
???????????
InputStreamReader ir=new
InputStreamReader(process.getInputStream());
???????????
BufferedReader input = new BufferedReader (ir);
???????????
String line;
???????????
while ((line = input.readLine ()) != null){
???????????????
System.out.println(line);
????????
}//end try
???????
catch (java.io.IOException e){
???????????
System.err.println ("IOException " + e.getMessage());

?

Java 可以通過 Runtime 調(diào)用Linux命令,形式如下:

Runtime.getRuntime().exec(command)

但是這樣執(zhí)行時(shí)沒有任何輸出,因?yàn)檎{(diào)用 Runtime.exec 方法將產(chǎn)生一個(gè)本地的進(jìn)程,并返回一個(gè)Process子類的實(shí)例(注意:Runtime.getRuntime().exec(command)返回的是一個(gè)Process類的實(shí)例)該實(shí)例可用于控制進(jìn)程或取得進(jìn)程的相關(guān)信息。

由于調(diào)用 Runtime.exec 方法所創(chuàng)建的子進(jìn)程沒有自己的終端或控制臺(tái),因此該子進(jìn)程的標(biāo)準(zhǔn)IO(如stdin,stdou,stderr)都通過 Process.getOutputStream(),Process.getInputStream(), Process.getErrorStream() 方法重定向給它的父進(jìn)程了。

用戶需要用這些stream來向子進(jìn)程輸入數(shù)據(jù)或獲取子進(jìn)程的輸出,下面的代碼可以取到 linux 命令的執(zhí)行結(jié)果:

try {
String[] cmd = new String[]{”/bin/sh”, “-c”, ” ls “};
Process ps = Runtime.getRuntime().exec(cmd);

BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append(”\n”);
}
String result = sb.toString();

System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}

=========================================================================

最后,補(bǔ)充一個(gè)RunShellUtil工具類:

package moni;import java.io.InputStreamReader; import java.io.LineNumberReader;/*** 調(diào)用shell
*/ public class RunShellUtil {/*** 執(zhí)行shell命令 String[] cmd = { "sh", "-c", "lsmod |grep linuxVmux" }或者* String[] cmd = { "sh", "-c", "./load_driver.sh" } */public static String runScript(String[] cmd) {StringBuffer buf = new StringBuffer();String rt = "-1";try {Process pos = Runtime.getRuntime().exec(cmd);pos.waitFor();InputStreamReader ir = new InputStreamReader(pos.getInputStream());LineNumberReader input = new LineNumberReader(ir);String ln = "";while ((ln = input.readLine()) != null) {buf.append(ln + "\n");}rt = buf.toString();input.close();ir.close();} catch (java.io.IOException e) {rt = e.toString();} catch (Exception e) {rt = e.toString();}return rt;}/*** 執(zhí)行簡單命令 String cmd="ls"*/public static String runScript(String cmd) {StringBuffer buf = new StringBuffer();String rt = "-1";try {Process pos = Runtime.getRuntime().exec(cmd);pos.waitFor();InputStreamReader ir = new InputStreamReader(pos.getInputStream());LineNumberReader input = new LineNumberReader(ir);String ln = "";while ((ln = input.readLine()) != null) {buf.append(ln + "\n");}rt = buf.toString();input.close();ir.close();} catch (java.io.IOException e) {rt = e.toString();} catch (Exception e) {rt = e.toString();}return rt;}/*** */public static void runC() {}public static void main(String args[]) {System.out.println(runScript("ls"));} }

?

轉(zhuǎn)載于:https://www.cnblogs.com/DarrenChan/p/6068403.html

超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生

總結(jié)

以上是生活随笔為你收集整理的java代码运行linux shell操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。