Java, Caused by: java.io.IOException: error=2, No such file or directory
Asked Answered
D

5

11
java.io.IOException: Cannot run program "yarn": error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at java.lang.Runtime.exec(Runtime.java:620)
    at java.lang.Runtime.exec(Runtime.java:450)
    at java.lang.Runtime.exec(Runtime.java:347)
    at com.Main.main(Main.java:32)

Hi there, I have a weird problem when execute a command using Java. I used the following code to run a command, if I run mkdir testFolder, the program run successful, but if I changed the command like yarn -v then error happen.

try {
    Process process = Runtime.getRuntime().exec("yarn -v");//, null, new File("/Users/macos/Desktop/TestProj/"));
} catch (IOException ex) {
    ex.printStackTrace();
}

P/s: In debug mode: IntelliJ could run above code, but Netbeans failed.

In production mode (jar file): IntelliJ failed too.

Edit 2:

  MACs-MacBook-Pro:~ macos$ which pwd
    /bin/pwd
    MACs-MacBook-Pro:~ macos$ which mkdir
    /bin/mkdir
    MACs-MacBook-Pro:~ macos$ which java
    /usr/bin/java
    MACs-MacBook-Pro:~ macos$ which yarn
    /usr/local/bin/yarn

I found that if I run a command that is in /bin or /usr/bin, the code run ok (pwd, mkdir, java -version ...), but yarn is in /usr/local/bin/, so it didn't work, and I still don't know how to fix.

Darius answered 18/2, 2019 at 7:46 Comment(18)
Do you have yarn installed on your machine?Infix
Have you tried separating the executable from its parameters? I don't know if that is an issue when using Runtime, but it is when using a ProcessBuilder.Antimere
@Infix yarn is installed and I can run that command using command lineDarius
@Antimere remove -v but still failed :(Darius
OK, the you have excluded one cause of error... at least.Antimere
To understand why it runs from command line, but does not from java compare the path that is passing to exec syscall when running from cmd and java correapondingly. Use strace.Carvalho
@SomeName I just have a search for strace, but it may unavailable on macDarius
Ah, sorry. Did not take that into account... Probably there should be another tool for mac to trace system calls, but unfortunately I cannot suggest anything since I dont use mac.Carvalho
thanks @SomeName, I will try to find something similar to do thatDarius
in your command line did you run yarn using sudo or without sudo?Cannabin
Quick googling shows that DTrace can be used for the purpose. Also this might be related #31046075Carvalho
@SHAHAKASH got this error if I add sudo Error: sudo: no tty present and no askpass program specifiedDarius
above error means when you add sudo then password prompt open and not given. for you can add nopassword to a specific path or give root access in sudoer file in ubantu.Cannabin
in command line write command "which yarn" and give me the output.Cannabin
@SHAHAKASH The behavior would be really wierd then if the issue was about permession. EACCES error has different code (13).Carvalho
try this Runtime.getRuntime().exec(" /usr/local/bin/yarn -v");Cannabin
@SHAHAKASH I got Error: env: node: No such file or directoryDarius
@Darius take a look github.com/SublimeLinter/SublimeLinter/issues/…Cannabin
D
12

I finally found the answer, because the Process I start is different with the process of the terminal, so can't access /usr/local/bin, have to add -l to run the command as logged in user. Runtime.getRuntime().exec(new String[]{"bash", "-l", "-c", cmd}, null, new File(f))

Darius answered 28/2, 2019 at 10:11 Comment(0)
C
1

As error code 2 suggests you specified a path incorrectly. The error code corresponds to POSIX ENOENT and "No such file or directory" its char * strerror(int errno) representation.

To troubleshoot the problem by yourself you can read the manual page http://man7.org/linux/man-pages/man2/execve.2.html

The relevant section is:

ENOENT

The file filename or a script or ELF interpreter does not exist, or a shared library needed for the file or interpreter cannot be found.

To see with which particula path your process is strarted run java witj strace -f

Carvalho answered 18/2, 2019 at 7:58 Comment(0)
G
0

In my case, I was using an ANT script that at some point called javac task with a fork=true attribute. Inspired by maphongba008 answer I deleted that fork, and so I stopped getting this same error.

Gorey answered 5/3, 2020 at 6:2 Comment(0)
J
0

I too faced for the same "Process p = Runtime.getRuntime().exec(cmd);".

Other solutions didn't worked as everything was correct and as expected.

I was using the downloaded JDK as Installed JRE's in eclipse and facing error.

Then I tried to search the JDK which is installed by sudo apt which resides in

/usr/lib/jvm/java-11-openjdk-amd64

Upon selecting this JDK, everything started working fine.

enter image description here

Judi answered 19/8 at 13:50 Comment(0)
M
-3

This worked for me:

npm install -g yarn
Mcandrew answered 6/5, 2020 at 6:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.