0

I'm trying to run a script from Java. I tried doing this in my main class.

    ProcessBuilder pb = new ProcessBuilder("./test_exec.sh", "hello world");
    Process p = pb.start();

I'm building the project through maven and test_exec.sh is included as a resource. When I unarchive the jar, I see the test_exec.sh file at the root directory. Why can't Java see the file? I've also tried test_exec.sh without the ./ in front of it.

The error I get is:

 Cannot run program "./test_exec.sh": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)

I've also tried:

Path currentPath = Paths.get("", "test_exec.sh").toAbsolutePath();
ProcessBuilder pb = new ProcessBuilder(currentPath.toString(), "hello world");
Process p = pb.start();
Crystal
  • 28,460
  • 62
  • 219
  • 393
  • I think, you should paste stack trace of error if you get any. – Sabir Khan May 12 '17 at 15:28
  • Is your path correct ? Is the script file in class folder or jar folder ? Try to give absolute path manualy – Burak Akyıldız May 12 '17 at 15:36
  • I'm new to Java so what I do know is it's included as a resource in src/main/resources using Maven. The output jar that I unarchived does have the test_exec.sh script file in the root folder of that jar. – Crystal May 12 '17 at 15:37
  • You actually lack the executable (the shell executable), you should put the path to the shell first. – Jeremy Grand May 12 '17 at 15:38
  • @JeremyGrand is correct. Or give the scripts absolute path correct... – Burak Akyıldız May 12 '17 at 15:39
  • @JeremyGrand Is there a platform specific way to do that so it can be deployed to different run times? – Crystal May 12 '17 at 15:39
  • @Cristal I actually do that in my application (and compliant with both Windows and Unix OS), but it's not code I would show to someone who is "new to java". You should get the path to the shell executable file with which you run those sh files. You probably can declare it as an environment variable and inject it as a property in your class. – Jeremy Grand May 12 '17 at 15:43
  • @JeremyGrand So if the command, like `bash` is an environment variable, I wouldn't have to give the absolute path then? – Crystal May 12 '17 at 15:45
  • @Crystal Are you running your application directly from the jar file (java -jar)? – whbogado May 12 '17 at 15:45
  • @whbogado The end goal would be yes. Right now I'm just running it from Intellij. – Crystal May 12 '17 at 15:46
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  May 12 '17 at 15:48
  • 1
    @Crystal The IDE runs the jar file. The operating system will not look inside a jar file for the script. To run a script like that you must extract it to be accessible from the file system. No matter how you set the path it won't work. – whbogado May 12 '17 at 15:50
  • @Cristal The `ProcessBuilder` won't parse your environment variables to get the proper path on its own, but you can, however, get the path from the `BASH_HOME` environment variable using `System.getenv("BASH_HOME")`. – Jeremy Grand May 12 '17 at 15:50
  • @JeremyGrand can you answer the question with gettig BASH_HOME as an environment variable. I got it to work. Thanks. – Crystal May 12 '17 at 16:49
  • @Crystal I tried to, but the question has been (wrongly, in my opinion) closed as a duplicate before I could answer. That's why I only answered in comments. – Jeremy Grand May 15 '17 at 07:27

0 Answers0