简体   繁体   English

Netbeans的Java-.jar意外问题

[英]Java - .jar unexpected issue with Netbeans

First of all, I am aware of Stack Overflow (and any competent forum-like website) policy of "search first, ask last", and, doing my homework, I searched various sources to find a solution to my issue. 首先,我了解“首先搜索,最后询问”的Stack Overflow(以及类似论坛的任何合格网站)政策,在做作业时,我搜索了各种资源以找到解决问题的方法。 That said, I, failing to find any suitable answers, was left no choice but to ask this problem personally. 就是说,我没有找到任何合适的答案,别无选择,只能亲自问这个问题。

I have somewhat moderate programming skills, especially regarding the Java language. 我有一定程度的编程技巧,尤其是在Java语言方面。 I am working on this 2D game with the default Java SE JDK. 我正在使用默认的Java SE JDK进行2D游戏。 More specifically JDK 7u4. 更具体地说,是JDK 7u4。 In this project, we have a class that manages most I/O operations. 在这个项目中,我们有一个类来管理大多数I / O操作。 One of its methods returns the path to a file: 其方法之一返回文件的路径:

public static URL load(String resource) {
    return ZM.class.getResource(resource);
}

Now, this method works fine when running the project on Netbeans (version 7.1). 现在,在Netbeans(版本7.1)上运行项目时,此方法可以正常工作。 However, when building and cleaning the project, the resulting .jar file does not seem to agree with its creator. 但是,在构建和清理项目时,生成的.jar文件似乎与创建者不一致。 When running the .jar on command line, the JVM caught a NullPointerException . 在命令行上运行.jar时,JVM捕获了NullPointerException It seemed that the file was not being able to be read inside the .jar. 似乎无法在.jar中读取文件。 Following my programmers instinct, I started debugging the project. 按照程序员的本能,我开始调试该项目。 My first attempt was to check whether the load method was the faulty member. 我的第一次尝试是检查load方法是否是错误的成员。 I ran some tests and obtained a couple of interesting results: 我进行了一些测试,并获得了一些有趣的结果:

When running the application on Netbeans and with "ZM.class" as the methods argument, it returned: 在Netbeans上以“ ZM.class”作为方法参数运行应用程序时,它返回:

/D:/Projects/GeometryZombiesMayhem/build/classes/geometryzombiesmayhem/ZM.class

But when running it from the .jar file, it returned: 但是从.jar文件运行它时,它返回:

file:/D:/Projects/GeometryZombiesMayhem/dist/GeometryZombiesMayhem.jar!/geometryzombiesmayhem/ZM.class

Naturally, I tried removing the initial file: string from it. 自然,我尝试从中删除初始file:字符串。 No effect. 没有效果。 Then I tried taking the exclamation mark from [...].jar![...] . 然后,我尝试从[...].jar![...]获得感叹号。 Again, nothing. 再说一遍。 I tried removing all the possible permutations from the path. 我尝试从路径中删除所有可能的排列。 No luck. 没运气。

Testing the method against the very own .jar file worked okay. 针对自己的.jar文件测试该方法可以正常进行。 Now, when I try to access the inside of the file, it doesn't let me. 现在,当我尝试访问文件内部时,它不允许我访问。 On earlier versions of this project it worked just fine. 在该项目的早期版本中,它工作得很好。 I am not really sure of what is going on. 我不太确定发生了什么。 Any help is welcome. 欢迎任何帮助。

Thank you in advance, Renato 预先谢谢你,雷纳托

When loading resources from a jar file, I've always used a classLoader. 从jar文件加载资源时,我一直使用classLoader。 Everything seems to work the same whether you run from within the IDE, launch the executable jar file or run the program from a web site using JNLP. 无论是从IDE中运行,启动可执行jar文件还是使用JNLP从网站运行程序,一切似乎都一样。

Try loading the resource this way instead: 尝试以这种方式加载资源:

 try {
      ClassLoader cl = ZM.getClass().getClassLoader();     
      ImageIcon img  = new ImageIcon(cl.getResource("images/programIcon.jpg"));
      // do stuff with img.
 }
 catch(Exception failed) {
     System.out.println(failed);
 }

One more suggestion - you should create a separate folder for resources. 另一个建议-您应该为资源创建一个单独的文件夹。 In my example above, images is a folder inside of my src folder. 在上面的示例中,图像是src文件夹内的文件夹。 This way it will automatically become part of the jar when I build it, but I am keeping resources separate from source code. 这样,在构建它时它将自动成为jar的一部分,但是我将资源与源代码分开。

I suppose your problem is in loading an image from your jar file. 我想您的问题是从jar文件加载图像。

Here is how i do it 这是我的方法

URL imageurl = Myclassanme.class.getResource("/test/Ergophobia.jpg"); 
Image myPicture = Toolkit.getDefaultToolkit().getImage(imageurl);
JLabel piclabel = new JLabel(new ImageIcon( myPicture ));
piclabel.setBounds(0,0,myPicture.getWidth(null),myPicture.getHeight(null));

This way I can get the Ergophobia.jpg file inside 'test' package. 这样,我可以在“测试”包中获取Ergophobia.jpg文件。

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

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