简体   繁体   English

为什么我的 JAR 文件在 CMD 处执行,而不是在双击时执行?

[英]Why does my JAR file execute at CMD, but not on double-click?

So I've been writing a simple 3D GUI application that I intended for users to use simply by double-clicking on the JAR file.因此,我一直在编写一个简单的 3D GUI 应用程序,供用户通过双击 JAR 文件来使用。 I got it working perfectly before putting it into the JAR file, and I got it working perfectly IN the JAR file while running from command prompt (typing "java -jar Modeler.jar" while in the directory of the jar file).在将它放入 JAR 文件之前,我让它工作得很好,我让它在 JAR 文件中工作得很好,同时从命令提示符运行(在 Z68995FCBF.042492D15Z8 文件的目录中键入“java -jar Modeler.jar”) However, when I double-click it, nothing happens.但是,当我双击它时,什么也没有发生。 It runs perfectly fine with no errors from command prompt.它运行得非常好,命令提示符没有错误。 I know from experience that crash reports on start-up are not shown because the console doesn't appear (or it disappears too fast), but when running from the command prompt there are no crash reports.根据经验,我知道启动时的崩溃报告没有显示,因为控制台没有出现(或者它消失得太快),但是从命令提示符运行时没有崩溃报告。 Any ideas as to why it won't work?关于为什么它不起作用的任何想法? I'm running Windows 7 Home Premium.我正在运行 Windows 7 Home Premium。 Here are the contents of the JAR file if it helps:如果有帮助,这里是 JAR 文件的内容:

Modeler.jar
|
+--*all the class files necessary*
|
+--META-INF
   |
   +--MANIFEST.MF

Contents of MANIFEST.MF: MANIFEST.MF 的内容:

Manifest-Version: 1.0
Built-By: AnonymousJohn
Class-Path: bin/j3dcore.jar bin/j3dutils.jar bin/vecmath.jar
Created-By: 1.6.0_16 (Sun Microsystems Inc.)
Main-Class: Start

EDIT: So after messing with the file associations to use java.exe instead of javaw.exe (thereby providing a window for print-outs), then modifying the startup mechanism a little to print out the current working directory, I discovered that the jar is running from "C:\Windows\system32" instead of the folder on my desktop I put it in. Go figure.编辑:因此在弄乱文件关联以使用 java.exe 而不是 javaw.exe (从而提供 window 用于打印输出)之后,然后稍微修改启动机制以打印出当前工作目录,我发现 Z6285048ZDAC04A99从“C:\Windows\system32”而不是我放入的桌面上的文件夹运行。Go 图。 However, moving the necessary outside files there doesn't help anything.但是,将必要的外部文件移到那里并没有任何帮助。

EDIT 2: I tried making another JAR file, this time with a simple JFrame with a button in it that tells you the current working directory.编辑 2:我尝试制作另一个 JAR 文件,这次是一个简单的 JFrame ,其中有一个按钮,告诉你当前的工作目录。 Press the button and it opens a (useless) JFileChooser.按下按钮,它会打开一个(无用的)JFileChooser。 This worked on double-click no matter where I put it in my computer.无论我把它放在电脑的哪个位置,这都适用于双击。 So there must be something wrong with my JAR file.所以我的 JAR 文件肯定有问题。 I'll start troubleshooting my program again.我将再次开始对我的程序进行故障排除。

EDIT 3: The problem is just what I thought it was: it's not loading libraries correctly when I double click on it.编辑3:问题正是我认为的:当我双击它时,它没有正确加载库。 The weird part is that in my tests where I display the current path and library path, the output is exactly the same whether I run it via command prompt or via double-clicking on it.奇怪的是,在我显示当前路径和库路径的测试中,output 无论是通过命令提示符还是通过双击运行它都完全相同。 Here's the stack trace:这是堆栈跟踪:

java.lang.UnsatisfiedLinkError: no j3dcore-d3d in java.library.path
  at java.lang.ClassLoader.loadLibrary(Unknown Source)
  at java.lang.Runtime.loadLibrary0(Unknown Source)
  at java.lang.System.loadLibrary(Unknown Source)
  at javax.media.j3d.NativePipeline$1.run(NativePipeline.java:231)
  at java.security.AccessController.doPrivileged(Native Method)
  at javax.media.j3d.NativePipeline.loadLibrary(NativePipeline.java:200)
  at javax.media.j3d.NativePipeline.loadLibraries(NativePipeline.java:157)
  at javax.media.j3d.MasterControl.loadLibraries(MasterControl.java:987)
  at javax.media.j3d.VirtualUniverse<clinit>(VirtualUniverse.java:299)
  at javax.media.j3d.Canvas3D.<clinit>(Canvas3D.java:3881)
  at ModelPreview.<init>(ModelPreview.java:51)
  at Modeler.<init>(Modeler.java:76)
  at Modeler.main(Modeler.java:1227)
  at Start.main(Start.java:92)

Only problem is that it IS in the library path.唯一的问题是它在库路径中。 I specifically set it in the program.我在程序中专门设置了。 Now that I think about it that may be the problem.现在想想,这可能是问题所在。 I set it like so (this was a method I found somewhere on the internet. I don't remember where):我是这样设置的(这是我在互联网上某个地方找到的一种方法。我不记得在哪里):

//above was code to get newPath based on the Operating System.
//all this code is set in a try-catch phrase.
//reset the library path
System.setProperty("java.library.path", ".\\bin\\natives" + newPath + ";");
//make sure the ClassLoader rereads the NEW path.
Field f = ClassLoader.class.getDeclaredField("sys_paths");
f.setAccessible( true );
f.set(null, null); //ClassLoader will automatically reread the path when it sees that it is null.  

EDIT FINAL: Well, after looking and relooking at my code, I discovered the problem was in some BS'ery involving the detection of 64-bit systems where it was loading the wrong dll's.编辑最后:好吧,在查看并重新查看我的代码之后,我发现问题出在一些涉及检测加载错误 dll 的 64 位系统的 BS'ery 中。 Why it worked from the command-line and not via double-click I don't know and will probably never know, but it works via double-click now, so I'm happy.为什么它从命令行工作而不是通过双击我不知道并且可能永远不会知道,但它现在通过双击工作,所以我很高兴。 Sorry about the troubles.很抱歉给您带来麻烦。

Ok, so I was stuck on this exact issue for over a week now (it has been for a side project for which I could only devote a few hours a day).好的,所以我在这个确切的问题上被困了一个多星期(这是一个我每天只能投入几个小时的副项目)。

This happened on my desktop, but for some reason wouldn't happen on the laptop.这发生在我的台式机上,但由于某种原因不会发生在笔记本电脑上。

After looking around, I found this answer and I thought I'd share it for people who are like me, found nothing useful in the accepted answer here.环顾四周后,我找到了这个答案,我想我会与像我一样的人分享它,在这里接受的答案中没有发现任何有用的东西。 Credits go to anonymous Stack Overflow user, whom username I've lost in all the excitement.将 go 归功于匿名 Stack Overflow 用户,我在所有兴奋中都失去了他的用户名。

As mentioned by some other answer somewhere to an seeming unrelated issue, use this little program to associate your JAR files to the 64 bit version of java:正如其他一些对看似无关问题的回答所提到的,使用这个小程序将您的 JAR 文件与 64 位版本的 java 相关联:

http://johann.loefflmann.net/en/software/jarfix/index.html http://johann.loefflmann.net/en/software/jarfix/index.html

Save the program somewhere and run it from command line with the parameter /64: c://path//jarfix.exe /64将程序保存在某处并使用参数 /64 从命令行运行它:c://path//jarfix.exe /64

Nothing else worked for me, but this was like magic.没有其他东西对我有用,但这就像魔术一样。 :) :)

The JAR is executable from CMD. JAR 可从 CMD 执行。 That means the JAR itself is formed correctly.这意味着 JAR 本身是正确形成的。 Good.好的。

The only reason to fail now is that double-click produces not a right command.现在失败的唯一原因是双击产生的命令不正确。 Expected command, as you correctly said, is正如您所说,预期的命令是

java -jar Modeler.jar

But when you associate javaw.exe with a JAR extension, I suspect it executes但是,当您将 javaw.exe 与 JAR 扩展关联时,我怀疑它会执行

javaw Modeler.jar

It is easy to check: make a javajar.cmd file, containing the following很容易检查:制作一个 javajar.cmd 文件,包含以下内容

javaw -jar %*

and associate it with JAR.并将其与 JAR 关联。 If you app starts OK, I'm right.如果您的应用程序启动正常,我是对的。 Otherwise, sorry.否则,对不起。

I got fed up with not being able to modify file associations in Windows 7 Control Panel and edited the registry (NOTE: it's not a bad idea at all to set a restore point before proceeding if you think there's ANY chance you may screw up)(I neither set restore point nor screwed up):我厌倦了无法在 Windows 7 控制面板中修改文件关联并编辑了注册表(注意:如果您认为有可能搞砸,那么在继续之前设置还原点并不是一个坏主意)(我既没有设置还原点也没有搞砸):

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\jar_auto_file]
@=""
"EditFlags"=hex:00,00,00,00

[HKEY_CLASSES_ROOT\jar_auto_file\shell]

[HKEY_CLASSES_ROOT\jar_auto_file\shell\open]

[HKEY_CLASSES_ROOT\jar_auto_file\shell\open\command]
@="\"C:\\Program Files\\Java\\jdk1.7.0_60\\bin\\javaw.exe\" -jar \"%1\" %*"

This assumes that .jar files have (Default) value jar_auto_file .这假设.jar文件具有(Default)jar_auto_file If not do this:如果不这样做:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.jar]
@="jar_auto_file"

ps One problem I found in the registry was that jarfile was associated with Netbeans, Chrome, and java.exe as well as with Applications\java.exe . ps 我在注册表中发现的一个问题是jarfile与 Netbeans、Chrome 和 java.exe 以及Applications\java.exe相关联。 I deleted that entire node (after exporting it, just so I could put it back if it was essential; it wasn't).我删除了整个节点(在导出它之后,如果它是必要的,我可以把它放回去;它不是)。 Now there is NO jarfile in my registry and all.jar files execute as before.现在我的注册表中没有jarfile并且 all.jar 文件像以前一样执行。

This manifests itself by showing more than one file type associated with .jar files in Control Panel.这通过在控制面板中显示与.jar文件关联的多个文件类型来表现出来。 You want this:你要这个:

在此处输入图像描述

If there's more than one item shown in Recommended programs, search registry for jarfile and (export first, then) delete that node:如果在推荐程序中显示了多个项目,请在注册表中搜索jarfile并(先导出,然后)删除该节点:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.jar

Just to put my two cents in, I had this problem and it was caused by something-or-other in the manifest, I suppose.只是为了把我的两分钱放进去,我有这个问题,我想它是由清单中的某些东西引起的。 I resolved it by generating the executable JAR in a different manner:我通过以不同的方式生成可执行文件 JAR 来解决它:

Previous developer generated the Runnable JAR from Eclipse (right-click project -> Export... -> Runnable JAR) and this worked fine for everyone.以前的开发人员从 Eclipse 生成了 Runnable JAR(右键单击项目 -> 导出... -> 可运行 JAR),这对每个人都很好。 Then I came along and generated the runnable JAR by configuring a Maven plugin ("assembly") in the pom.xml and so a classic Maven build would produce the runnable JAR. Then I came along and generated the runnable JAR by configuring a Maven plugin ("assembly") in the pom.xml and so a classic Maven build would produce the runnable JAR.

This worked nicely for me (from cmd and by double-clicking) but not for the end-users (who could still run the tool from cmd, but not by double-clicking the JAR).这对我很有效(来自 cmd通过双击)但不适用于最终用户(他们仍然可以从 cmd 运行该工具,但不能通过双击 JAR)。

I generated the runnable JAR from Eclipse and now everyone is happy again.我从 Eclipse 生成了可运行的 JAR,现在每个人都高兴了。 Not sure why the maven-generated JAR wasn't ok to run by double-clicking, but I can't be bothered to investigate further now that the job is done.不知道为什么 Maven 生成的 JAR 不能通过双击运行,但既然工作已经完成,我就懒得进一步调查了。 Hope this half-advice helps someone.希望这个半建议对某人有所帮助。

I had same issue with.jar files after trying everything i realized that I have more than one versions of Java installed.在尝试了所有我意识到我安装了多个版本的 Java 之后,我遇到了与.jar 文件相同的问题。 Removing the unnecessary versions solved my issue.删除不必要的版本解决了我的问题。 May be because the.jar was confused between different versions of JRE.可能是因为.jar 混淆了不同版本的JRE。

Prerequisite: make sure JRE(that is included in JDK) is installed.先决条件:确保已安装 JRE(包含在 JDK 中)。

Solution解决方案

  1. Open regedit and navigate to Computer\HKEY_CLASSES_ROOT\jar_auto_file\shell\open\command打开 regedit 并导航到Computer\HKEY_CLASSES_ROOT\jar_auto_file\shell\open\command

  2. Add "-jar" to value data.将“-jar”添加到值数据。 For example, change from "C:\Program Files\Java\jdk-15.0.1\bin\java.exe" "%1" to "C:\Program Files\Java\jdk-15.0.1\bin\java.exe" "-jar" "%1"例如,从"C:\Program Files\Java\jdk-15.0.1\bin\java.exe" "%1"更改为"C:\Program Files\Java\jdk-15.0.1\bin\java.exe" "-jar" "%1"

  3. Done完毕

If problem still occurs, make sure you configure correct main class in project property.如果问题仍然存在,请确保在项目属性中配置正确的主 class。

Just so that the answer to the question is clear to anyone passing by, I'll put my solution here (I couldn't before because of the 8-hour rule):只是为了让路过的任何人都清楚问题的答案,我将把我的解决方案放在这里(由于 8 小时规则,我以前不能这样做):

Well, after looking and relooking at my code, I discovered the problem was in some BS'ery involving the detection of 64-bit systems where it was loading the wrong dll's.好吧,在查看并重新查看我的代码之后,我发现问题出在一些涉及检测 64 位系统的 BS'ery 中,它加载了错误的 dll。 Why it worked from the command-line and not via double-click I don't know and will probably never know, but it works via double-click now, so I'm happy.为什么它从命令行工作而不是通过双击我不知道并且可能永远不会知道,但它现在通过双击工作,所以我很高兴。 Sorry about the troubles.很抱歉给您带来麻烦。

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

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