0

I have 2 methods I wrote to try and execute a Jar file from my Java application and they both are not doing anything. The Java Runtime Environment is installed on the C: drive and by default its Path points to a directory on the C: drive. The Jar file I am trying to execute is located on the E: drive.

Jar location: E:\Demo Folder\MyDemo.jar

I tried to execute MyDemo.jar using the following 2 methods:

Method 1:

try {
    Runtime.getRuntime().exec("cmd /c start %SystemDrive%\\java -jar " + "E:/Demo Folder/MyDemo.jar");
} catch (IOException ex) {
    System.err.println(ex.getMessage());
}

Method 2:

try {
    File dirFile = new File("E:/Demo Folder/");
    ProcessBuilder pb = new ProcessBuilder("java", "-jar", "E:/Demo Folder/MyDemo.jar");
    pb.directory(dirFile);
    Process p = pb.start();
} catch (Exception ex) {
    System.err.println(ex.getMessage());
}
jadrijan
  • 1,438
  • 4
  • 31
  • 48
  • Try to do it like this: http://stackoverflow.com/questions/4936266/execute-jar-file-from-a-java-program – sorencito Dec 12 '12 at 19:28
  • I would do that but I do not know always know what version of java is installed and to what location. In his example his got /path/to/java – jadrijan Dec 12 '12 at 19:30
  • 1
    Then try to read the path to Java from the system properties. Should be Java.home – sorencito Dec 12 '12 at 19:32
  • 1
    Just a guess, but could it be you need to set the environment variable for the project (assuming you are using Eclipse). In other words I bet the second one would work if you ran your app from the command line. – Jacob Schoen Dec 12 '12 at 19:34
  • I am using NetBeans, I will try both suggestions. – jadrijan Dec 12 '12 at 19:48

3 Answers3

1

Didn't you try to put your invocation logic inside a, say, E:/Demo Folder/rundemo.bat` (or .cmd) file, and call that .bat from your java instead? That's usually more sane and easy to troubleshoot.

leonbloy
  • 73,180
  • 20
  • 142
  • 190
1

I'm guessing the problem is the space in the path of the jar file. Try this:

new ProcessBuilder("java", "-jar", "\"E:/Demo Folder/MyDemo.jar\"");
Zagrev
  • 2,000
  • 11
  • 8
1

To launch a external java executable application you must first locate the java.exe file (Which loads the JVM) and pass the argument of -jar to indicate loading of a executable JAR file. In both methods you provided you had minor errors within your code.

In method one:

cmd /c start %SystemDrive%\\java -jar

%SystemDrive% is being treated as a string literal as java is unaware of Windows specific environment variables.

In method two:

"java", "-jar", "E:/Demo Folder/MyDemo.jar"

You are assuming that java.exe has been added to PATH environmental variables which may not be the case. Also, from your usage of the "%" operators, I am assuming you are on a windows machine, which uses \ for directories therefore... "E:/Demo Folder/MyDemo.jar" may not return a valid location.

Try the following segment of code:

try {
    File dirFile = new File("E:\\Demo Folder\\");
    ProcessBuilder pb = new ProcessBuilder(System.getProperty("java.home") + File.separator + "bin" + File.separator + "java", "-jar", new File(dirFile, "MyDemo.jar").getAbsolutePath());
    pb.directory(dirFile);
    Process p = pb.start();
} catch (Exception ex) {
    System.err.println(ex.getMessage());
}
initramfs
  • 8,275
  • 2
  • 36
  • 58