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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

java 执行cd_Java调用Linux命令(cd的处理)

發布時間:2025/3/20 linux 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 执行cd_Java调用Linux命令(cd的处理) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

這是一個非常常用的調用方法示例:public String executeLinuxCmd(String cmd) {

System.out.println("got cmd job : " + cmd);

Runtime run = Runtime.getRuntime();

try {

Process process = run.exec(cmd);

InputStream in = process.getInputStream();

BufferedReader bs = new BufferedReader(new InputStreamReader(in));

// System.out.println("[check] now size \n"+bs.readLine());

StringBuffer out = new StringBuffer();

byte[] b = new byte[8192];

for (int n; (n = in.read(b)) != -1;) {

out.append(new String(b, 0, n));

}

System.out.println("job result [" + out.toString() + "]");

in.close();

// process.waitFor();

process.destroy();

return result;

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

二、含有管道符(|)多級命令串聯查詢public List executeLinuxCmd(String cmd) {

System.out.println("got cmd job : " + cmd);

Runtime run = Runtime.getRuntime();

try {

// Process process = run.exec(cmd);

Process process = run.exec(new String[] {"/bin/sh", "-c", cmd});

InputStream in = process.getInputStream();

BufferedReader bs = new BufferedReader(new InputStreamReader(in));

List list = new ArrayList();

String result = null;

while ((result = bs.readLine()) != null) {

System.out.println("job result [" + result + "]");

list.add(result);

}

in.close();

// process.waitFor();

process.destroy();

return list;

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

三、含有cd操作的方法示例問題背景

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長連接,保持連接,發送多條命令,最后釋放連接。

示例邏輯代碼:public void executeNewFlow() {

Runtime run = Runtime.getRuntime();

File wd = new File("/bin");

System.out.println(wd);

Process proc = null;

try {

proc = run.exec("/bin/bash", null, wd);

} catch (IOException e) {

e.printStackTrace();

}

if (proc != null) {

BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);

out.println("cd /home/test");

out.println("pwd");

out.println("rm -fr /home/proxy.log");

out.println("exit");//這個命令必須執行,否則in流不結束。

try {

String line;

while ((line = in.readLine()) != null) {

System.out.println(line);

}

proc.waitFor();

in.close();

out.close();

proc.destroy();

} catch (Exception e) {

e.printStackTrace();

}

}

}

三的優化和演進(返回值)public List executeNewFlow(List commands) {

List rspList = new ArrayList();

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);

}

proc.waitFor();

in.close();

out.close();

proc.destroy();

} catch (IOException e1) {

e1.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

}

return rspList;

}

總結

以上是生活随笔為你收集整理的java 执行cd_Java调用Linux命令(cd的处理)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。