How to export working jar with non-jar dependencie

2019-07-25 06:20发布

im fairly new to java and im making a pokemon style game for practice and i would like to be able to send the game to my friends.

here is the main problem: the game works fine in my netbeans IDE, but using the jar file in my dist folder does not work and throws a nullPointerException. i have narrowed down the problem. my game uses imageIcons and png/gif images that i have imported in my libraries. im getting access to them like this

Icon bckground = new javax.swing.ImageIcon(getClass().getResource("/pictures/BG.gif"));

i am unsure how to get the images into the lib folder for the program to find. i have tried copying the files straight into the lib folder and creating a folder for them called pictures; neither worked. right now the lib folder contains only a single jar from one of my other libraries. (that is the only jar file that i am importing to my libraries)

Pic of what it looks like in IDE

2条回答
地球回转人心会变
2楼-- · 2019-07-25 06:49

Please put your picture (BG.gif) in the package (directory) where it is used as icon Icon bckground = new javax.swing.ImageIcon(getClass().getResource("BG.gif")); inside the jar file. You need to change the path of the file first getResource("BG.gif") and then create the jar file from IDE. If the jar does not contain the image, you can open the jar using using any unzip application (winrar etc.) and copy and paste the the image file in the directory where the class is present. Please let me know the outcome.

查看更多
何必那么认真
3楼-- · 2019-07-25 06:59

In my case, i did something like this.

final BufferedImage image = ImageIO.read(new File("something.jpg"));

I just transferred something.jpg into the dist folder, and it worked fine.

After clean-building your project, put the BG.gif into the dist folder, then run your jar file in dist folder. Now everything should be fine. When sending your game to your friends, you can encapsulte (hide, set read-only) your code (google encapsultaion java), then with the BG.gif being transferred into dist folder, archive the project. Then your friends only need to unzip it and find jar file in dist folder.

Hope this will help:)

查看更多
登录 后发表回答