Home > Back-end >  Runtime.exec("echo 2") gives error=13, but why?
Runtime.exec("echo 2") gives error=13, but why?

Time:03-29

I try to run commands using Java. This is the 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");
    }
}

This is the result:

[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$1.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$1.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]$ 

How to solve the Exception?

CodePudding user response:

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$ 

Your arguments to Runtime.exec should be the last ones above

CodePudding user response:

I have noticed that the library

/mnt/ramdisk/jdk-15.0.2/lib/jspawnhelper/jspawnhelper

did not had execution permissions. After I added execution permissions the code worked like a charm.

  • Related