gpt4 book ai didi

java - 从 Java 运行 Linux 命令

转载 作者:太空宇宙 更新时间:2023-11-04 09:39:42 25 4
gpt4 key购买 nike

我正在尝试从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com