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());
}