为什么我的 JAR 文件在 CMD 处执行,而不是在双击时执行? [英] Why does my JAR file execute at CMD, but not on double-click?

查看:16
本文介绍了为什么我的 JAR 文件在 CMD 处执行,而不是在双击时执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在编写一个简单的 3D GUI 应用程序,我打算让用户通过双击 JAR 文件来使用它.在将它放入 JAR 文件之前,我让它运行良好,并且在从命令提示符运行时(在 jar 文件的目录中键入java -jar Modeler.jar")时,我让它在 JAR 文件中运行良好.但是,当我双击它时,没有任何反应.它运行得非常好,命令提示符下没有错误.我从经验中知道启动时的崩溃报告没有显示,因为控制台没有出现(或者它消失得太快),但是从命令提示符运行时没有崩溃报告.关于为什么它不起作用的任何想法?我正在运行 Windows 7 家庭高级版.如果有帮助,以下是 JAR 文件的内容:

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. 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). 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. Here are the contents of the JAR file if it helps:

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

MANIFEST.MF 的内容:

Contents of 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

所以在搞乱文件关联以使用 java.exe 而不是 javaw.exe(从而提供打印输出的窗口)之后,然后稍微修改启动机制以打印出当前工作目录,我发现jar 是从C:Windowssystem32"运行的,而不是我把它放在桌面上的文件夹.去图.但是,将必要的外部文件移到那里没有任何帮助.

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:Windowssystem32" instead of the folder on my desktop I put it in. Go figure. However, moving the necessary outside files there doesn't help anything.

编辑 2:我尝试制作另一个 JAR 文件,这次使用一个简单的 JFrame,其中有一个按钮,告诉您当前的工作目录.按下按钮,它会打开一个(无用的)JFileChooser.无论我把它放在我的电脑的什么地方,这都适用于双击.所以我的JAR文件肯定有问题.我将再次开始对我的程序进行故障排除.

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. Press the button and it opens a (useless) 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. I'll start troubleshooting my program again.

编辑 3:问题正是我所想的:当我双击它时,它没有正确加载库.奇怪的是,在我显示当前路径和库路径的测试中,无论是通过命令提示符运行还是通过双击运行,输出都完全相同.这是堆栈跟踪:

EDIT 3: The problem is just what I thought it was: it's not loading libraries correctly when I double click on it. 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. 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:嗯,在查看并重新查看我的代码后,我发现问题出在某些 BS'ery 中,涉及检测 64 位系统,其中加载了错误的 dll.为什么它从命令行工作而不是通过双击我不知道并且可能永远不会知道,但它现在通过双击工作,所以我很高兴.很抱歉给您带来麻烦.

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. 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.

推荐答案

为了让路过的任何人都清楚问题的答案,我将我的解决方案放在这里(我之前不能,因为 8-小时规则):

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):

好吧,在看了又看了之后我的代码,我发现问题是在一些涉及检测的BS'ery它正在加载的 64 位系统错误的dll.为什么它起作用命令行而不是通过双击我不知道并且会可能永远不知道,但它通过现在双击,所以我很高兴.对不起关于烦恼.

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. 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.

这篇关于为什么我的 JAR 文件在 CMD 处执行,而不是在双击时执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆