java 执行cd_Java调用Linux命令(cd的处理)
一、Java調(diào)用Linux系統(tǒng)的命令非常簡單
這是一個非常常用的調(diào)用方法示例: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;
}
二、含有管道符(|)多級命令串聯(lián)查詢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 調(diào)用上面的接口兩次?executeLinuxCmd("cd /home/test");
executeLinuxCmd("rm -fr /home/proxy.log");
是不行的!
1.4 這個接口的調(diào)用是單次事務型的,就是每次調(diào)用都是獨立的事務或者說操作,沒有關聯(lián)的。
那這種“復雜”一點的操作流程怎么辦呢?
1.5 方法a: 可以寫一個獨立的腳本,然后一次運行腳本,這樣多復雜的邏輯都沒問題。
1.6 方法b: 可以啟動一個shell長連接,保持連接,發(fā)送多條命令,最后釋放連接。
示例邏輯代碼: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");//這個命令必須執(zhí)行,否則in流不結(jié)束。
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();
}
}
}
三的優(yōu)化和演進(返回值)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");// 這個命令必須執(zhí)行,否則in流不結(jié)束。
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;
}
總結(jié)
以上是生活随笔為你收集整理的java 执行cd_Java调用Linux命令(cd的处理)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bst java_BST(二叉搜索树)
- 下一篇: java的图形界面上传附件_Java图形