1

I'm writing a Java program MyAwesomeProgram that uses Process' exec function to run bash commands locally. My code is located in /home/a/b/c, and there are .sh files located in /home/a/b/d that I need to run. However, when I run my code:

Process p;
Runtime rt = new Runtime.getRuntime();
p = rt.exec("./home/a/b/d/shell.sh");
p.waitFor();

I receive an error:

Exception in thread "main" java.io.IOException: Cannot run program "./home/a/b/d/shell.sh": java.io.IOException: error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at MyAwesomeProgram.main(MyAwesomeProgram.java:186)
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.<init>(UNIXProcess.java:148)
at java.lang.ProcessImpl.start(ProcessImpl.java:65)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)

I believe this is just a mistake in formatting the exec command String, however I haven't been able to find a solution thus far. Where have I messed up? Any other tips/tricks for using exec effectively would be appreciated, but completely optional. Thanks!

Edit: I got the code working, it was an issue with a couple directory references I got backwards as well as what Woot4Moo said.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Chris
  • 1,465
  • 1
  • 18
  • 36
  • did you try without the .? if your code is running from /home/a/b/c, then thats /home/a/b/c/home/a/b/d/shell.sh – Dan Apr 10 '12 at 18:05
  • post how you run this from the command line please. Including your current directory (i.e. /home/chris) – Woot4Moo Apr 10 '12 at 18:19
  • javac MyAwesomeProgram.java, java MyAwesomeProgram. The main method contains the code (this is a dummy program to test exec functionality before I incorporate it into a larger already-tested group of programs). Everything necessary is already imported and tested to work as expected. – Chris Apr 10 '12 at 18:21
  • Oh sorry if I misunderstood and you mean the actual command and not the program I've run it from /home/a/b/c/ as "../d/script.sh" as you posted in your answer below. – Chris Apr 10 '12 at 18:32

1 Answers1

3

well if your program lives in:

/home/a/b/c

and your scripts live in:

/home/a/b/d

and you use the . you are not in the right directory. You want to exec it with the following path:

../d/script.sh

The . says use the current directory + your string. So in essence your input is the following:

/home/a/b/c/home/a/b/d

The .. allows you to go up one directory which if you are at :

/home/a/b/c

you want then arrive at:

/home/a/b
Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
  • I updated the code to reflect your suggestion and I get the same java.io.IOException, except with "../home/a/b/d/script.sh" despite it working manually in Terminal. Any thoughts? – Chris Apr 10 '12 at 18:12
  • what are the permissions on the file? And what user are you running the code as? – Woot4Moo Apr 10 '12 at 18:14
  • The permissions are 755, and I have near-root privledges but not root. I should be able to manipulate/delete/etc all files in question, so I don't think it's a permissions issue. The entire code is just creating a Process, Runtime, and then the exec calls so there isn't much else I could be leaving out. – Chris Apr 10 '12 at 18:20