我有一个要在终端 OSX 上运行的命令:
cat File1 | ./huawei2text.pl > ~/File2.txt && cat ~/File2.txt| tr "," "\n" > ~/Output.txt
如何仅使用 java 运行此命令?
我试过这段代码:
String whatToRun = "cat File1 | ./File.pl > ~/File2.txt && cat ~/File2.txt| tr "," "\n" > ~/Output.txt";
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(whatToRun);
int exitVal = proc.waitFor();
System.out.println("Process exitValue:" + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
更新
答案和解决方案:
String whatToRun = "cat File1 | ./File.pl > File2.txt "
+ "&& cat File2.txt| tr \",\" \"\n\" > Output.txt";
String[] shellcmd = {"/bin/sh", "-c", whatToRun};
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(shellcmd);
int exitVal = proc.waitFor();
System.out.println("Process exitValue:" + exitVal);
}
catch (Throwable t) {
t.printStackTrace();
}
现在它起作用了。谢谢!