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();
15
  • I think, you should paste stack trace of error if you get any. Commented May 12, 2017 at 15:28
  • Is your path correct ? Is the script file in class folder or jar folder ? Try to give absolute path manualy Commented May 12, 2017 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. Commented May 12, 2017 at 15:37
  • You actually lack the executable (the shell executable), you should put the path to the shell first. Commented May 12, 2017 at 15:38
  • 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. Commented May 12, 2017 at 15:50

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.