[英]How to invoke a Linux shell command from Java
我正在嘗試使用重定向 (>&) 和管道 (|) 從 Java 執行一些 Linux 命令。 Java 如何調用csh
或bash
命令?
我試着用這個:
Process p = Runtime.getRuntime().exec("shell command");
但它與重定向或管道不兼容。
exec 不會在你的 shell 中執行命令
嘗試
Process p = Runtime.getRuntime().exec(new String[]{"csh","-c","cat /home/narek/pk.txt"});
反而。
編輯:: 我的系統上沒有 csh,所以我改用了 bash。 以下對我有用
Process p = Runtime.getRuntime().exec(new String[]{"bash","-c","ls /home/XXX"});
使用 ProcessBuilder 來分隔命令和參數而不是空格。 無論使用何種外殼,這都應該有效:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(final String[] args) throws IOException, InterruptedException {
//Build command
List<String> commands = new ArrayList<String>();
commands.add("/bin/cat");
//Add arguments
commands.add("/home/narek/pk.txt");
System.out.println(commands);
//Run macro on target
ProcessBuilder pb = new ProcessBuilder(commands);
pb.directory(new File("/home/narek"));
pb.redirectErrorStream(true);
Process process = pb.start();
//Read output
StringBuilder out = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null, previous = null;
while ((line = br.readLine()) != null)
if (!line.equals(previous)) {
previous = line;
out.append(line).append('\n');
System.out.println(line);
}
//Check result
if (process.waitFor() == 0) {
System.out.println("Success!");
System.exit(0);
}
//Abnormal termination: Log command parameters and output and throw ExecutionException
System.err.println(commands);
System.err.println(out.toString());
System.exit(1);
}
}
以@Tim 的示例為基礎,創建一個獨立的方法:
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Shell {
/** Returns null if it failed for some reason.
*/
public static ArrayList<String> command(final String cmdline,
final String directory) {
try {
Process process =
new ProcessBuilder(new String[] {"bash", "-c", cmdline})
.redirectErrorStream(true)
.directory(new File(directory))
.start();
ArrayList<String> output = new ArrayList<String>();
BufferedReader br = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = null;
while ( (line = br.readLine()) != null )
output.add(line);
//There should really be a timeout here.
if (0 != process.waitFor())
return null;
return output;
} catch (Exception e) {
//Warning: doing this is no good in high quality applications.
//Instead, present appropriate error messages to the user.
//But it's perfectly fine for prototyping.
return null;
}
}
public static void main(String[] args) {
test("which bash");
test("find . -type f -printf '%T@\\\\t%p\\\\n' "
+ "| sort -n | cut -f 2- | "
+ "sed -e 's/ /\\\\\\\\ /g' | xargs ls -halt");
}
static void test(String cmdline) {
ArrayList<String> output = command(cmdline, ".");
if (null == output)
System.out.println("\n\n\t\tCOMMAND FAILED: " + cmdline);
else
for (String line : output)
System.out.println(line);
}
}
(測試示例是一個命令,它按時間順序遞歸地列出目錄及其子目錄中的所有文件。)
順便說一句,如果有人能告訴我為什么我需要四個和八個反斜杠,而不是兩個和四個,我可以學到一些東西。 比我所計算的還要多一層無法逃避的事情發生。
編輯:剛剛在 Linux 上嘗試了相同的代碼,結果我在測試命令中需要一半的反斜杠! (即:預期的 2 和 4 數量。)現在它不再只是奇怪,而是一個可移植性問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.