Reputation: 133
I want to call a compiler (nonstandard) located in a folder inside my Java application. So I wrote this code:
Process p = Runtime.getRuntime().exec("closures/bin/javac " + filename);
It worked! But now I want to pack this compiler along with my .class files in a .jar file. My folder structure is something like that:
.class (a lot of classes)
closures/ (a folder)
But when the line mentioned above from the jar file (the call to the compiler in a subfolder) is executed, I got this:
Exception in thread "main" java.io.IOException: Cannot run program "closures/bin/javac": java.io.IOException: error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:475)
at java.lang.Runtime.exec(Runtime.java:610)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at Main.main(Main.java:44)
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.<init>(UNIXProcess.java:164)
at java.lang.ProcessImpl.start(ProcessImpl.java:81)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:468)
... 4 more
It seem to me a path problem.
What am I doing wrong?
Thanks.
Upvotes: 3
Views: 1199
Reputation: 26910
You cannot execute an executable in the jar file. You must extract it before calling Runtime.getRuntime().exec()
.
Think again, Runtime.getRuntime().exec()
use your OS's function. Your OS don't know about .jar
file. You have to extract it.
Upvotes: 2
Reputation: 48559
Just from your vague description, why do you think that your app's pwd is the same folder that closures is in? It looks to me like you're running it from .class, so you'll probably want AT LEAST
Process p = Runtime.getRuntime().exec("../closures/bin/javac " + filename);
but that's still not the most robust way of calling it.
Upvotes: 0