Java 从外部目录运行 Shell 脚本:没有这样的文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25647806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running Shell Script From External Directory: No such file or directory
提问by ickarsim
I have a shell script file that i want to run from java. My java work space directory is different than the script's directory.
我有一个我想从 java 运行的 shell 脚本文件。我的 java 工作空间目录与脚本的目录不同。
private final String scriptPath = "/home/kemallin/Desktop/";
public void cleanCSVScript() {
String script = "clean.sh";
try {
Process awk = new ProcessBuilder(scriptPath + script).start();
awk.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
and i get this error:
我得到这个错误:
java.io.IOException: Cannot run program "cat /home/kemallin/Desktop/capture-03.csv | awk -F ',' '{ print ,",", ,",", ,",", ,",", }' > clean.csv": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
at ShellScript.cleanCSVScript(ShellScript.java:21)
at Main.main(Main.java:15)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:186)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028)
... 2 more
java.io.FileNotFoundException: /home/kemallin/Desktop/clean.csv (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at java.io.FileReader.<init>(FileReader.java:58)
at CSVReader.run(CSVReader.java:25)
at Main.main(Main.java:17)
I have googled it and every solution pretty much indicate that i'm doing the right thing.
我已经用谷歌搜索了它,每个解决方案几乎都表明我在做正确的事情。
I have tried to put the script file in the src and in bin of the Java project but still it says no such file or dir.
我试图将脚本文件放在 src 和 Java 项目的 bin 中,但它仍然说没有这样的文件或目录。
What am i doing wrong?
我究竟做错了什么?
Thanks.
谢谢。
采纳答案by David P. Caldwell
Your program clean.sh
is not an executable as Java understands it, even though the underlying system understands it as executable.
您的程序clean.sh
不是 Java 理解的可执行文件,即使底层系统将其理解为可执行文件。
You need to tell Java what shell is needed to execute your command. Do (assuming you are using bash
and it is installed at /bin/bash
):
您需要告诉 Java 需要什么 shell 来执行您的命令。执行(假设您正在使用bash
并且安装在/bin/bash
):
private final String scriptPath = "/home/kemallin/Desktop/";
public void cleanCSVScript() {
String script = "clean.sh";
try {
Process awk = new ProcessBuilder("/bin/bash", scriptPath + script).start();
awk.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
回答by Romeo Kienzler
You should do a chmod 755 /home/kemallin/Desktop/clean.sh
and ensure the java process is run under the same userid
您应该执行chmod 755 /home/kemallin/Desktop/clean.sh
并确保 java 进程在相同的用户 ID 下运行