Runtime.exec("echo 2") 给出错误=13,但为什么呢?
Runtime.exec("echo 2") gives error=13, but why?
我尝试使用 Java 运行 命令。这是 class:
package test;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
Runtime.getRuntime().exec("/usr/bin/echo 2");
}
}
这是结果:
[grim@obelix dev.xxx /mnt/ramdisk/workspace/test]$ /usr/bin/echo 2
2
[grim@obelix dev.xxx /mnt/ramdisk/workspace/test]$ /mnt/ramdisk/apache-maven-3.8.5/bin/mvn exec:java -Dexec.mainClass=test.Test
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------------< test:test >------------------------------
[INFO] Building test 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ test ---
[WARNING]
java.io.IOException: Cannot run program "/usr/bin/echo": error=13, Keine Berechtigung
at java.lang.ProcessBuilder.start (ProcessBuilder.java:1142)
at java.lang.ProcessBuilder.start (ProcessBuilder.java:1073)
at java.lang.Runtime.exec (Runtime.java:591)
at java.lang.Runtime.exec (Runtime.java:415)
at java.lang.Runtime.exec (Runtime.java:312)
at test.Test.main (Test.java:8)
at org.codehaus.mojo.exec.ExecJavaMojo.run (ExecJavaMojo.java:254)
at java.lang.Thread.run (Thread.java:832)
Caused by: java.io.IOException: error=13, Keine Berechtigung
at java.lang.ProcessImpl.forkAndExec (Native Method)
at java.lang.ProcessImpl.<init> (ProcessImpl.java:313)
at java.lang.ProcessImpl.start (ProcessImpl.java:244)
at java.lang.ProcessBuilder.start (ProcessBuilder.java:1109)
at java.lang.ProcessBuilder.start (ProcessBuilder.java:1073)
at java.lang.Runtime.exec (Runtime.java:591)
at java.lang.Runtime.exec (Runtime.java:415)
at java.lang.Runtime.exec (Runtime.java:312)
at test.Test.main (Test.java:8)
at org.codehaus.mojo.exec.ExecJavaMojo.run (ExecJavaMojo.java:254)
at java.lang.Thread.run (Thread.java:832)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.029 s
[INFO] Finished at: 2022-03-27T11:39:28+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:java (default-cli) on project test: An exception occured while executing the Java class. Cannot run program "/usr/bin/echo": error=13, Keine Berechtigung -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[grim@obelix 1 dev.xxx /mnt/ramdisk/workspace/test]$
如何解决异常?
goose@t410:/tmp$ ls /usr/bin/echo
ls: cannot access '/usr/bin/echo': No such file or directory
goose@t410:/tmp$ type echo
echo is a shell builtin
goose@t410:/tmp$ /bin/bash -c "echo 2"
2
goose@t410:/tmp$
你对 Runtime.exec
的论点应该是上面的最后一个
我注意到图书馆
/mnt/ramdisk/jdk-15.0.2/lib/jspawnhelper/jspawnhelper
没有执行权限。在我添加执行权限后,代码运行得非常棒。
我尝试使用 Java 运行 命令。这是 class:
package test;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
Runtime.getRuntime().exec("/usr/bin/echo 2");
}
}
这是结果:
[grim@obelix dev.xxx /mnt/ramdisk/workspace/test]$ /usr/bin/echo 2
2
[grim@obelix dev.xxx /mnt/ramdisk/workspace/test]$ /mnt/ramdisk/apache-maven-3.8.5/bin/mvn exec:java -Dexec.mainClass=test.Test
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------------< test:test >------------------------------
[INFO] Building test 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ test ---
[WARNING]
java.io.IOException: Cannot run program "/usr/bin/echo": error=13, Keine Berechtigung
at java.lang.ProcessBuilder.start (ProcessBuilder.java:1142)
at java.lang.ProcessBuilder.start (ProcessBuilder.java:1073)
at java.lang.Runtime.exec (Runtime.java:591)
at java.lang.Runtime.exec (Runtime.java:415)
at java.lang.Runtime.exec (Runtime.java:312)
at test.Test.main (Test.java:8)
at org.codehaus.mojo.exec.ExecJavaMojo.run (ExecJavaMojo.java:254)
at java.lang.Thread.run (Thread.java:832)
Caused by: java.io.IOException: error=13, Keine Berechtigung
at java.lang.ProcessImpl.forkAndExec (Native Method)
at java.lang.ProcessImpl.<init> (ProcessImpl.java:313)
at java.lang.ProcessImpl.start (ProcessImpl.java:244)
at java.lang.ProcessBuilder.start (ProcessBuilder.java:1109)
at java.lang.ProcessBuilder.start (ProcessBuilder.java:1073)
at java.lang.Runtime.exec (Runtime.java:591)
at java.lang.Runtime.exec (Runtime.java:415)
at java.lang.Runtime.exec (Runtime.java:312)
at test.Test.main (Test.java:8)
at org.codehaus.mojo.exec.ExecJavaMojo.run (ExecJavaMojo.java:254)
at java.lang.Thread.run (Thread.java:832)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.029 s
[INFO] Finished at: 2022-03-27T11:39:28+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:java (default-cli) on project test: An exception occured while executing the Java class. Cannot run program "/usr/bin/echo": error=13, Keine Berechtigung -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[grim@obelix 1 dev.xxx /mnt/ramdisk/workspace/test]$
如何解决异常?
goose@t410:/tmp$ ls /usr/bin/echo
ls: cannot access '/usr/bin/echo': No such file or directory
goose@t410:/tmp$ type echo
echo is a shell builtin
goose@t410:/tmp$ /bin/bash -c "echo 2"
2
goose@t410:/tmp$
你对 Runtime.exec
的论点应该是上面的最后一个
我注意到图书馆
/mnt/ramdisk/jdk-15.0.2/lib/jspawnhelper/jspawnhelper
没有执行权限。在我添加执行权限后,代码运行得非常棒。