我想从下面的程序中运行一个shell脚本,但似乎没有任何反应。在linux终端中直接运行相同的命令是可以工作的,所以我猜测是我的Java代码有问题。正如您所看到的,我首先使用PrintWriter将命令写入shell脚本,但我希望这不会影响shell脚本本身的运行。希望能得到帮助!
public static void main(String[] args) {
// TODO Auto-generated method stub
String nfdump = "nfdump -o csv -r /home/shane/Documents/nfdump/nfcapd.201211211526>blank.txt";
try {
FileWriter fw = new FileWriter("/home/shane/Documents/script.sh");
PrintWriter pw = new PrintWriter(fw);
pw.println("#!/bin/bash");
pw.println(nfdump);
pw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Process proc = null;
try {
proc = Runtime.getRuntime().exec("sh /home/shane/Documents/script.sh");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
nfdump
。 - Miserable Variablescript.sh
是否为空吗? - Aaron Digulla