java jar双击运行,运行.jar文件 - 双击与命令行执行

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值