Skip to content
Advertisement

Why am I getting IOException : “Cannot run program ./shellScript.sh error = 2, No such file or directory in Java?

The following Main.java code simply tries to simulate the following linux command:

JavaScript

The program below works only if the executable Main.jar sits within /dir1/dir2, not outside of /dir1/dir2. How do I modify the program below so that Main.jar can sit anywhere on the file system?

JavaScript

Advertisement

Answer

You should use ProcessBuilder to launch or one of the overloads of exec. You need to specify the pathname to the script and pass the same pathname as the current directory to run the script in:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement