I have a java desktop application that contains the following code:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
System.out.println("check1");
int intResult = compiler.run(System.in, System.out, foutErrorFile, strvalidatePath);
System.out.println("check2");
When I run the corresponding .jar file of this application by executing "java -jar name.jar", both check1 and check2 gets printed and app works fine.
But when i try to run the jar by double clicking the .jar file, I found that ToolProvider.getSystemJavaCompiler() is returning null. "check2" does not get printed. I dont get proper result from compiler.run().
I did modify the registry entry "\HKEY_CLASSES_ROOT\jarfile\shell\open\command" from
"C:\Program Files\Java\jre1.6.0\bin\javaw.exe" -jar "%1" %* to
"C:\Program Files\Java\jre1.6.0\bin\java.exe" -jar "%1" %*.
This way I'm able to see the console when the app is running.
So why is my program (which runs fine while run using java -jar command) malfunctioning when I run the .jar file by double-clicking?
解决方案
I got my problem solved. While double-clicking, the command that gets executed is the one specified in registry entry. In my case the registry entry "\HKEY_CLASSES_ROOT\jarfile\shell\open\command" is:
"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
This means its uses the javaw.exe app in the JRE directory to execute the program. My JRE directory lacked one .jar file named Tools.jar in its lib folder. This was essential to acquire the compiler during the program execution.
I copied the missing jar file from the JDK directory lib folder to the same in JRE directory. This solved my problem. Thank you to all for helping.