2

I currently have the following problem:

I have two java applications: updater and client. The updater is a native application (which contains the necessary JDK), while the client is just a runnable jar file.

When the user starts the application, it basically starts the updater, which searches for an update for the client application on the server. When a new update is present, the updater downloads the update and places it in a file (or overwrites the current jar file). Then, I execute the downloaded .jar file like this:

private void startApplication() {

    Runtime rt = Runtime.getRuntime();
    try {
        Process pr = rt.exec("java -jar libs/myJar.jar");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This works fine when building and running in Eclipse, but when I run the native application (which is built by Eclipse) myJar.jar doesn't get executed. I think it is due to the fact that the updater can not find the jar file, because it absolute path becomes different.

My question: How can I make sure that the native application can execute the downloaded jar file?

Note: I am currently building the app on Mac OSX, so I am testing this with a .dmg. I don't know if the problem is also present for Windows or Linux.

Edit 1: It doesn't seem to be a problem with the path, because this seems to be fine (as tested with @Funtik, thank you for that).

Edit 2: The file permissions also seem to be correct. Maybe it has something to do with the fact that the client jar doesn't know which JVM to use?

bashoogzaad
  • 4,611
  • 8
  • 40
  • 65

1 Answers1

2

You have to use absolute path for this.

 String path = new File(".").getAbsolutePath();
 Process pr = rt.exec("java -jar "+ path + "/libs/myJar.jar");
WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52
  • Thanks for your reply! I just tried it, but it doesn't seem to work. It does work while running it from the IDE, but not when I compile the native application and run it. – bashoogzaad Dec 04 '14 at 10:07
  • try to output the path to console and see where you're actually located – WeMakeSoftware Dec 04 '14 at 10:08
  • This is the path: /Applications/MyApp.app/Contents/Java/. , which seems to be okay. – bashoogzaad Dec 04 '14 at 10:30
  • so the full path to the client.jar is the following? /Applications/MyApp.app/Contents/Java/libs/myJar.jar? – WeMakeSoftware Dec 04 '14 at 10:32
  • Yes, that it correct. When I run the jar from inside the package, everything seems to work fine. So maybe there is something wrong with the package. – bashoogzaad Dec 04 '14 at 10:32
  • check the file permissions inside .dmg if the java is allowed to read the jar file – WeMakeSoftware Dec 05 '14 at 07:07
  • The file permissions are all correct (read/write). Maybe it has something to do with the fact that the downloaded client jar does not know which JVM to use? – bashoogzaad Dec 06 '14 at 16:11
  • Are you able to launch the client, but it fails in runtime? – WeMakeSoftware Dec 06 '14 at 20:54
  • Yes, I can launch it manually from within the folder, but the runtime fails. – bashoogzaad Dec 06 '14 at 20:55
  • please post the full stacktrace – WeMakeSoftware Dec 06 '14 at 20:56
  • Where can I find the stacktrace when I run a native application? I am not able to find the Java console anywhere. Any ideas? – bashoogzaad Dec 06 '14 at 20:58
  • I'm lost here. You are launching the process from the java code. So this code gets executed in a java runtime, right? Or am I missing something? – WeMakeSoftware Dec 06 '14 at 21:02
  • Okay I finally found a way to view the java console, and it does not give a specific error. However, I printed the "path" string as you suggested it in your example, and it prints the home folder of my mac, which is obviously not right! What am I doing wrong? – bashoogzaad Dec 08 '14 at 21:10
  • that means that the JVM is started in your home folder, not inside the .dmg file. I guess you have to know the absolute path to the dmg file, to get everything working – WeMakeSoftware Dec 09 '14 at 12:55