简体   繁体   English

Java应用程序将从CMD和Eclipse运行,但不能双击

[英]Java application will run from CMD and Eclipse but not double click

I've done a search and I can see that a lot of people have had the same problem as me, but none of the solutions have worked for me. 我做了一个搜索,我可以看到很多人遇到了和我一样的问题,但没有一个解决方案对我有用。

Basically I have a Java Project in Eclipse that is from my old Windows Installation. 基本上我在Eclipse中有一个来自旧Windows安装的Java项目。 I've cleaned and rebuilt it because at first it wouldn't compile, but now I have it exported as a Runnable Jar. 我已经清理并重建了它,因为起初它不会编译,但现在我将它导出为Runnable Jar。 However, the only way I can get the application to appear is to do java -jar foo.jar in command prompt, or run it in Eclipse. 但是,我可以让应用程序出现的唯一方法是在命令提示符下执行java -jar foo.jar ,或者在Eclipse中运行它。 If I double click the JAR in Windows Explorer nothing happens even though I know that Java is associated correctly because other Runnable Jars work. 如果我在Windows资源管理器中双击JAR,即使我知道Java正确关联,因为其他Runnable Jars工作,也没有任何反应。

The project only has the x86 JRE listed in it's Build Path Libraries and all the files listed appear to exist. 该项目仅在其构建路径库中列出了x86 JRE,并且列出的所有文件似乎都存在。 I'm running Windows 7 HP. 我正在运行Windows 7 HP。

Update : I'm sorry, but I just discovered that no other Runnable Jars are working either. 更新 :对不起,我刚刚发现没有其他Runnable Jars正在工作。 If they are wrapped with launch4j they work though... 如果他们用launch4j包裹,他们工作虽然...

Edit : The Runnable Jars that I export from Eclipse do work fine on other systems and load on double click 编辑 :我从Eclipse导出的Runnable Jars在其他系统上运行正常,并在双击时加载

Some registry values or file associations are probably messed up. 某些注册表值或文件关联可能搞砸了。 Wiping off all of your existing JRE's and JDK's and re-installing them should fix your issue. 擦除所有现有的JRE和JDK并重新安装它们应该可以解决您的问题。


Alternatively you may be able to fix it by manually editing the registry value here: HKLM > SOFTWARE > Classes > jarfile > shell > open > command My value is 或者,您可以通过在此处手动编辑注册表值来修复它: HKLM > SOFTWARE > Classes > jarfile > shell > open > command我的值是

Type: REG_SZ 键入: REG_SZ

Data: "C:\\Program Files\\Java\\jre8\\bin\\javaw.exe" -jar "%1" %* 数据: "C:\\Program Files\\Java\\jre8\\bin\\javaw.exe" -jar "%1" %*

You'd of course want that path to point to your javaw.exe, and make sure you have the additional arguments. 您当然希望该路径指向您的javaw.exe,并确保您有其他参数。

I was also facing the same problem while i was working with Spring tool suite. 我在使用Spring工具套件时也遇到了同样的问题。

You may use the following steps:- 您可以使用以下步骤: -

  • Right click on project -> export -> Runnable jar file -> (Here,In library handling,there are three options,you have to choose middle one ie package required library into generated jar.It will package external dependency also). 右键单击项目 - >导出 - > Runnable jar文件 - >(这里,在库处理中,有三个选项,你必须选择中间一个,即将所需的包打包到生成的jar中。它也将打包外部依赖项)。

-In my case, my runnable jar was only executing on my environment ie on which i have created that JAR. - 在我的情况下,我的runnable jar只在我的环境上执行,即我已经创建了JAR。 Initially i have selected the first option to create JAR ie extract required libraries into required JAR.but that was not proper. 最初我选择了第一个创建JAR的选项,即将所需的库提取到所需的JAR中。但这不合适。

It may help you.Let me correct if i am getting wrong. 它可能对你有所帮助。如果我出错了,请让我纠正。

Since you are able to run the JAR running the command line, I believe your issue is related to which version of Java is set to run the file when it is double-clicked. 由于您可以运行运行命令行的JAR,因此我认为您的问题与双击时设置的运行文件的Java版本有关。

To find out which version is successfully running the file from the command line and set it to open JAR files by default: 要查找从命令行成功运行该文件的版本,并将其设置为默认打开JAR文件:

  1. Open a new command prompt window. 打开一个新的命令提示符窗口。
  2. Run echo %path% . 运行echo %path% Among the path values, you should be able to find one pointing to the bin folder of one of the installed versions of Java. 在路径值中,您应该能够找到一个指向其中一个已安装Java版本的bin文件夹的路径值。 Copy this path somewhere. 在某处复制此路径。
  3. Navigate to the JAR file you would like to run. 导航到您要运行的JAR文件。 Right click the JAR -> Open with -> Choose default program... -> Browse... 右键单击JAR - >打开 - >选择默认程序... - >浏览...
  4. Browse to the path you copied in step 2. (the easiest way is to paste it into the address bar) 浏览到您在步骤2中复制的路径。(最简单的方法是将其粘贴到地址栏中)
  5. Double click javaw.exe. 双击javaw.exe。
  6. Click OK. 单击确定。

You should now be able to run the JAR file. 您现在应该能够运行JAR文件。 Please let me know if your problem persists. 如果您的问题仍然存在,请与我们联系。

I have made a jar with and keep it on desktop.Then,I double clicked on the jar and it working fine for me. 我已经制作了一个罐子并将其保存在桌面上。然后,我双击罐子,它对我来说很好。

How I and what I have monintored: In my main class, perform some operation and at the end I add on Thread.sleep(25000); 我和我所拥有的东西:在我的主类中,执行一些操作,最后我添加了Thread.sleep(25000); to hold the program for few moments. 暂时保持该计划。 After every double click on the exported jar I found one new javaw.exe process added in the system process tree. 每次双击导出的jar后,我发现在系统进程树添加了一个新的javaw.exe进程 I have noticed it on Task manager. 我在任务管理器上注意到了它。 and after 25000ms respective javaw.exe process ended. 各个javaw.exe进程结束后25000ms。 As my application does not cointain any GUI that's why I have not seen any GUI changes for the respective process. 因为我的应用程序没有结合任何GUI,这就是为什么我没有看到相应进程的任何GUI更改。 I'm Confident that if my application have some GUI, I will surely get the respective GUI window on every run. 我很自信,如果我的应用程序有一些GUI,我肯定会在每次运行时获得相应的GUI窗口。

Common mistakes : when we export a project from Eclipse as Runnable JAR file, it is exported with selected Eclipse launch configuration and Eclipse specific launch wrappers. 常见错误:当我们将项目从Eclipse导出为Runnable JAR文件时,它将使用选定的Eclipse启动配置和Eclipse特定的启动包装器导出。 Now if the configuration does not match when you are trying to run it via double-click you will not be able to see the error, If you run it from CMD then surely you will get the error log. 现在,如果配置不匹配,当您尝试通过双击运行它时,您将无法看到错误,如果您从CMD运行它,那么您肯定会得到错误日志。

To create standard executable JAR file : To create a standard executable JAR file, you can export as JAR file and specify the main class in last screen of the wizard. 要创建标准可执行JAR文件:要创建标准可执行JAR文件,可以导出为JAR文件并在向导的最后一个屏幕中指定主类

That's working for me fine. 这对我很有用。 I used the same jar from different system and keeping it different location. 我使用了来自不同系统的相同jar并保持不同的位置。

Really silly idea here.. 这里真的很傻。

Have you tried elevating the privileges of whatever you are opening the .jar with (java.exe or javaw.exe)? 您是否尝试使用(java.exe或javaw.exe)提升打开.jar的权限?

Right click on the program -> Properties -> Compatibility tab -> Check the "Run this program as administrator" button at the bottom. 右键单击程序 - >属性 - >兼容性选项卡 - >选中底部的“以管理员身份运行此程序”按钮。

There is a possibility that you have elevated privileges on your command window, which may be letting it run. 您可能在命令窗口中拥有提升的权限,这可能会让它运行。

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

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