I have a Calculator.jar file that I created with Netbeans.
I want to run that jar file using java code. After looking around stackoverflow and some documentation sites, I tried the following:
public static void main (String [] args){
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("java -jar C:\\pathToJar\\Calculator.jar);
pr.waitFor();
}
The code runs fine, but my problem comes in with the path to the file. If I run the program without the absolute path, it runs and finishes, but I never see the calculator GUI pop up. When I run with the absolute path, the GUI pops up as expected.
I have the jar file in the same place as my source code. I thought doing that would allow me to just have to put Calculator.jar without the absolute path. That doesn't seem to work though.
Does anyone know how I could possibly store the jar file so I don't need to use the absolute path?
Edit: I also tried creating a separate package within the project called jarFiles and tried the path jarFiles\\Calculator.jar
. That didn't work either. I think I'm misunderstanding something about the default path/what path cmd is using by default when I call the command. I'm not sure though.
Thanks for any advice in advance!
I believe the problem that you're having is that when you try
public static void main (String [] args){
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("java -jar \\Calculator.jar");
pr.waitFor();
}
It looks into the root directory of the computer, also, there was a quotation mark forgotten on the end. The way you should access the current working directory is by simply eliminating the \\\\
, you might also want to surround the jar file path in quotation marks, just in case.
public static void main (String [] args){
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("java -jar \"Calculator.jar\"");
pr.waitFor();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.