我正在尝试从 java 运行命令。但是命令没有执行。谁能指出原因?
String path = dest+"/info_updated.csv";
File file = new File(path);
String command = "sed -i '/" + subscriberId + "/d' "+file;
System.out.println("command "+command);
Runtime run = Runtime.getRuntime();
Process p = null;
try {
p = run.exec(command);
InputStream errorStream = p.getErrorStream();
p.waitFor();
} catch (IOException ioe) {
ioe.printStackTrace();
System.out.println("ERROR.RUNNING.CMD");
} catch (Exception e) {
e.printStackTrace();
} finally {
p.destroy();
}
return true;
}
在日志中它正确地打印了命令。
command sed -i '/L13876110226750000/d' /usr/share/apache-tomcat-6.0.37/webapps/SMS/info_updated.csv
但是没有执行。
最佳答案
你可以在命令前加上/bin/sh -c
试试看。
像这样:
String[] command = {"/bin/sh", "-c", "sed -i '/" + subscriberId + "/d' "+file};
Process p = run.exec(command);
关于java - 从 Java 运行 Linux 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22705689/