2

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?

Sam
  • 73
  • 9
  • Which operating system you are using? – Raider Aug 01 '14 at 11:32
  • Possible duplicate of [this](http://stackoverflow.com/questions/18434292/i-can-run-jar-files-through-cmd-but-i-cannot-double-click-them) – Mustafa sabir Aug 04 '14 at 12:36
  • I dont think it is, because I am actually being able to start the program and everything else seems to work fine by double-clicking including it's GUI. Except for this part where it invokes a compiler function. – Sam Aug 05 '14 at 03:48

2 Answers2

3

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.

Sam
  • 73
  • 9
-1

I think the best way is create a .bat file who calls java -jar name.jar

Raider
  • 394
  • 1
  • 2
  • 26
  • I cant do that because this software wasn't developed by me and this problems seems to be in my computer only. If there is any change i can make in my computer or any minor change in code which I can suggest, that would be more helpful. – Sam Aug 05 '14 at 07:34
  • I just found that ToolProvider.getSystemJavaCompiler() is returning null which is causing this problem. – Sam Aug 05 '14 at 07:44