views:

353

answers:

4

Hi everyone!

I recently created an application and successfully jarred this to c:/my/folder/app.jar. It works like a charm in the following case [Startup #1]:

  • Open cmd
  • cd to c:/my/folder
  • java -jar app.jar

But when I do this, it doesn't work [Startup #2]:

  • Open cmd
  • cd to c:/my/
  • java -jar folder/app.jar

Because app.jar contains a .exe-file which I try to run in my application:

final Process p = Runtime.getRuntime().exec("rybka.exe");

It won't work in example 2 because it can't find the file rybka.exe.

Any suggestions?

+1  A: 

If the jar will always be in that directory you can use a full path /my/folder/rybka.exe. If not, you can use getClass().getProtectionDomain().getCodeSource().getLocation() to find out the location of the jar and prepend that onto rybka.exe.

Cahlroisse
Does this work if the .exe is contained within the .jar ?
Brian Agnew
A: 

That obviously doesn't work ("file:/C:/Users/anton/Desktop/LiveEngine.jarrybka.exe"). How to actually go INTO the jar? (The .exe-file ofcourse is within the .jar-file)

Exception in thread "main" java.io.IOException: Cannot run program "file:/C:/Users/anton/Desktop/LiveEngine.jarrybka.exe": CreateProcess error=2, The system cannot find the file specified

  • at java.lang.ProcessBuilder.start(Unknown Source)
  • ...
  • at java.lang.Runtime.exec(Unknown Source)
  • at Main.(Main.java:31)
  • at Main.main(Main.java:22)

Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified

See if this helps then: http://stackoverflow.com/questions/600146/run-exe-which-is-packaged-inside-jar
Cahlroisse
You should use the comments or edit your question and. Don't create an answer for extra clarification.
the_drow
+1  A: 

Try extracting the exe to

System.getProperty("java.io.tmpdir"));

then run it from this location too should work every time.

Paul

Paul Whelan
+3  A: 

Something like this is a better way forward. Copy the exe out of the jar to a temp location and run it from there. Your jar will then also be executable via webstart and so on:

InputStream src = MyClass.class.getResource("rybka.exe").openStream();
File exeTempFile = File.createTempFile("rybka", ".exe");
FileOutputStream out = new FileOutputStream(exeTempFile);
byte[] temp = new byte[32768];
int rc;
while((rc = src.read(temp)) > 0)
    out.write(temp, 0, rc);
src.close();
out.close();
exeTempFile.deleteOnExit();
Runtime.getRuntime().exec(exeTempFile.toString());
Jon Bright