简体   繁体   English

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

[英]Running .jar file - Double click vs. command line execution

I have a java desktop application that contains the following code:我有一个包含以下代码的 Java 桌面应用程序:

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.当我通过执行“java -jar name.jar”运行此应用程序的相应 .jar 文件时,check1 和 check2 都被打印出来并且应用程序工作正常。 But when i try to run the jar by double clicking the .jar file, I found that ToolProvider.getSystemJavaCompiler() is returning null.但是当我尝试通过双击 .jar 文件来运行 jar 时,我发现 ToolProvider.getSystemJavaCompiler() 返回 null。 "check2" does not get printed. “check2”不会被打印出来。 I dont get proper result from compiler.run().我没有从 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" %*.我确实将注册表项 "\\HKEY_CLASSES_ROOT\\jarfile\\shell\\open\\command" 从 "C:\\Program Files\\Java\\jre1.6.0\\bin\\javaw.exe" -jar "%1" %* 修改为 "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?那么为什么我的程序(在使用 java -jar 命令运行时运行良好)在我双击运行 .jar 文件时出现故障?

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:在我的情况下,注册表项“\\HKEY_CLASSES_ROOT\\jarfile\\shell\\open\\command”是:

"C:\\Program Files\\Java\\jre7\\bin\\javaw.exe" -jar "%1" %* "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.这意味着它使用 JRE 目录中的 javaw.exe 应用程序来执行程序。 My JRE directory lacked one .jar file named Tools.jar in its lib folder.我的 JRE 目录的 lib 文件夹中缺少一个名为 Tools.jar 的 .jar 文件。 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.我将丢失的 jar 文件从 JDK 目录 lib 文件夹复制到 JRE 目录中的相同文件。 This solved my problem.这解决了我的问题。 Thank you to all for helping.感谢大家的帮助。

我认为最好的方法是创建一个调用java -jar name.jar的 .bat 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM