Eclipse cannot load images after exporting Jar package?
The reason for this problem is mostly because of the path problem, which is often run normally in the project, but once packaged into a Jar, it cannot be displayed normally. Let’s summarize the methods to solve this problem:
1. Solve by using external resource files
After packaging the project to generate Jar, create a new folder and place all the image files used in the project and generated Jar files in this directory. As long as the code references the image files correctly, such as: ImageIcon image=new ImageIcon(SwingResourceManager.getImage("icons/logo.gif"));
In this way, the image file and the class file are not placed in the package together. The class file is in the package, and the image file is in the project root directory.
2. Solve by using internal resource files
Put the image files used in the project package, and when packaging, the image files are entered into the Jar file at the same time. This method should use one of the following in the specific code:
ImageIcon image = new ImageIcon(this.class.getResource("icons/logo.gif"));The following is a detailed description of this method:
When you write a program for graphical interfaces, you must use various image resources. So, when you publish your program, how do you publish these image resources?
The simplest way is to package these image resources together with your class file into an executable Jar file, so that you only need to publish a jar file to include all the contents, and users can run the program just by double-clicking the Jar file. If you package pictures in a Jar file, how do you access these pictures when writing programs?
What are the paths for these pictures? You can use the getResource() method to solve this problem.
Here we analyze the two most common situations:
1. You package all resources into andy.jar, and your class is in a package: package andycpp; all your image resources are placed in the images folder, and the images folder is also in the andycpp package. In this way, the final path is:
andy.jar
|__andycpp
|__ GUITest.class
|__ images
|__ Snap1.jpg
Because the image folder and the class that references it are located at the same level, you can access it through a relative path:
java.net.URL imgURL = GUITest.class.getResource("images/Snap1.jpg");ImageIcon img1 = new ImageIcon(imgURL);What needs to be paid attention to here is case issues! The inside of the Jar file is case sensitive, so if the image itself is called snap1.jpg and you write the path to images/Snap1.jpg, then the image will not be found. It should also be noted that each class (note, not an object) has a getResource() method, but you cannot use it casually. You can only use the getResource() method of a class that is at the same level as the image folder. For example, in the above figure, the GUITest class and images are at the same level, so you need to use the getResource() method of this class.
2. In another case, if you have many classes and the package structure is very complicated, then it is unlikely that you will put the picture and the class at the same level, but you should put the picture at the outermost layer and let all classes access the picture through absolute paths:
andy.jar
|__andycpp
|__ GUITest.class
|__ images
|__ Snap1.jpg
The code is as follows:
java.net.URL imgURL = GUITest.class.getResource("/images/Snap1.jpg");ImageIcon img1 = new ImageIcon(imgURL);Have you seen the difference from the above code? The difference is very subtle. Just add a backslash "/" in front of "images". This backslash represents the root directory, and no backslash represents the relative path..
The above article briefly talks about the path problem of Java packaging images into jars is all the content I have shared with you. I hope it can give you a reference and I hope you can support Wulin.com more.