Running .sh files with Java's exec in a different directory?
Asked Answered
C

1

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.

Canonicals answered 10/4, 2012 at 18:2 Comment(4)
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.shArguelles
post how you run this from the command line please. Including your current directory (i.e. /home/chris)Sid
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.Canonicals
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.Canonicals
S
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
Sid answered 10/4, 2012 at 18:4 Comment(3)
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?Canonicals
what are the permissions on the file? And what user are you running the code as?Sid
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.Canonicals

© 2022 - 2024 — McMap. All rights reserved.