问题描述
我正在尝试从Java类中调用bash脚本.
I am trying to call a bash script from a java class .
这是我的Java程序
import java.io.File;
public class RunBuild {
public static void main(String[] args) {
File wd = new File("/home/sai/Jan5WS/ATCore/bin/");
System.out.println("Working Directory: " + wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec(" . Ram.sh", null, wd);
System.out.println(proc.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
}
我已经拥有该脚本的所有权限,请参见下文sai @ sai-Aspire-4720Z:〜/Jan5WS/ATCore/bin $ chmod 7777 Ram.sh
I have got all the permissions for that script , please see below sai@sai-Aspire-4720Z:~/Jan5WS/ATCore/bin$ chmod 7777 Ram.sh
-rwxrwxrwx 1 sai sai 77 Feb 3 20:53 Ram.sh~
-rwxrwxrwx 1 sai sai 79 Feb 3 20:53 Ram.sh
sai@sai-Aspire-4720Z:~/Jan5WS/ATCore/bin$
将以下异常抛出
Working Directory: /home/sai/Jan5WS/ATCore/bin
java.io.IOException: Cannot run program "." (in directory
"/home/sai/Jan5WS/ATCore/bin"): error=13, Permission denied
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at RunBuild.main(RunBuild.java:12)
Caused by: java.io.IOException: error=13, Permission denied
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 4 more
我正在使用Ubuntu请让我知道可能是什么问题?
I am using UbuntuPlease let me know what could be the problem ??
推荐答案
查看错误输出-您正在尝试执行."
,它是一个目录,而不是您的shell脚本:
See your error output - you are trying to execute "."
which is a directory, not your shell script:
java.io.IOException: Cannot run program "."
用"./"
替换 exec()
调用中的."
以指示当前目录,请确保您的脚本具有适当的shebang行,例如#!/bin/bash
,并且该脚本是可执行的(您已经这样做了):
Replace the " . "
in your exec()
call with "./"
to indicate the current directory, make sure that your script has the proper shebang line, like #!/bin/bash
, and that it is executable (which you already did):
proc = Runtime.getRuntime().exec("./Ram.sh", null, wd);
这篇关于Java调用bash脚本:权限被拒绝错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!