Java 应用程序可以在 netbeans 上正常工作,但不能在命令行或 .jar 文件中正常工作

发布于 2024-12-21 02:44:24 字数 1131 浏览 0 评论 0原文

我使用 netbeans 创建了一个应用程序,当我从 netbeans 运行它时,它运行得很好, 但是当我尝试从命令行或从可执行的 .jar 文件运行它时,它不起作用!在我看来,这个错误是:

Dec 12, 2011 11:36:33 PM game.Threads.Intro_main <init>
SEVERE: null
javax.imageio.IIOException: Can't read input file!
        at javax.imageio.ImageIO.read(Unknown Source)
        at game.Threads.Intro_main.<init>(Intro_main.java:58)
        at game.Intro.<init>(Intro.java:28)
        at game.Start.<init>(Start.java:25)
        at game.Start.main(Start.java:35)

Exception in thread "main" java.lang.NullPointerException
        at sun.awt.CustomCursor.<init>(Unknown Source)
        at sun.awt.windows.WCustomCursor.<init>(Unknown Source)
        at sun.awt.windows.WToolkit.createCustomCursor(Unknown Source)
        at game.Threads.Intro_main.<init>(Intro_main.java:69)
        at game.Intro.<init>(Intro.java:28)
        at game.Start.<init>(Start.java:25)
        at game.Start.main(Start.java:35)

我认为问题出在命令行和命令上。 .jar 无法确定我在应用程序中加载的图像的真实路径,因此我不知道避免此问题的正确方法。

我知道这里有很多关于这个问题的问题,但我不知道真正的解决方案,我尝试了很多答案,但都不起作用!

感谢您的合作:)

I created an application using netbeans, and it works very good when I run it from netbeans,
but when I try to run it from a command line or from the excutable .jar file, then it doesn't work! And this errors appears to me:

Dec 12, 2011 11:36:33 PM game.Threads.Intro_main <init>
SEVERE: null
javax.imageio.IIOException: Can't read input file!
        at javax.imageio.ImageIO.read(Unknown Source)
        at game.Threads.Intro_main.<init>(Intro_main.java:58)
        at game.Intro.<init>(Intro.java:28)
        at game.Start.<init>(Start.java:25)
        at game.Start.main(Start.java:35)

Exception in thread "main" java.lang.NullPointerException
        at sun.awt.CustomCursor.<init>(Unknown Source)
        at sun.awt.windows.WCustomCursor.<init>(Unknown Source)
        at sun.awt.windows.WToolkit.createCustomCursor(Unknown Source)
        at game.Threads.Intro_main.<init>(Intro_main.java:69)
        at game.Intro.<init>(Intro.java:28)
        at game.Start.<init>(Start.java:25)
        at game.Start.main(Start.java:35)

I think that the problem is the command line & the .jar doesn't determine the true path for images which I load in the application, so I don't know the right way to avoid this problem.

I know that there are many questions here about this problem, but I didn't know the true solution, I tried many answers and they didn't work!

Thank you for your cooperation :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤蝉 2024-12-28 02:44:24

避免这种情况的最佳方法是将要使用的图像打包到 .jar 文件中。然后使用类加载器加载图像。

javax.imageio.ImageIO.read(getClass().getResourceAsStream("myImage.png"));

The best way to avoid this is to package the images you want to use in the .jar file. Then use the classloader to load the images.

javax.imageio.ImageIO.read(getClass().getResourceAsStream("myImage.png"));
与君绝 2024-12-28 02:44:24

要解决此问题,只需通过 winrar 存档程序将 jar 文件提取到文件夹中即可。然后看看结果..你的外部文件不会在那里..所以你必须将你的外部文件与你的jar文件放在同一个文件夹中。 NullPointerException 问题是无法正确初始化您的 jar 文件。您必须设置具有 main 方法的 Main 类。

To solve this problem, simply extract jar file into a folder via winrar archive program. Then see the result.. Your external files wont be there.. So you have to put your external files with your jar file in the same folder. And NullPointerException problem is came out not correctly initialize your jar file.. You have to set your Main class which has main method.

猫卆 2024-12-28 02:44:24

如果您使用只读图像,则可以将它们保留在 jar 中并使用 URL getClass().getResource() 或 InputStream getClass().getResourceAsStream()。否则,您可以通过 System.getProperty("user.dir") 使用工作目录的路径。

主目录 System.getProperty("home.dir") 也经常被使用:例如 NetbeansIDE 的“/NetbeansProjects”。

If you use read-only images, you could leave them in the jar and use the URL getClass().getResource() or the InputStream getClass().getResourceAsStream(). Otherwise you can use the path of the working directory with System.getProperty("user.dir").

The home directory System.getProperty("home.dir") often also is used: for instance with "/NetbeansProjects" for the NetbeansIDE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文