1

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?

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98

3 Answers3

4

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
  • 13,388
  • 3
  • 31
  • 46
1

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
  • 536
  • 2
  • 5
1

Try extracting the exe to

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

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

Paul

Paul Whelan
  • 16,574
  • 12
  • 50
  • 83