Java to package a picture to a path problem in a jar __java

Source: Internet
Author: User

Eclipse uses the export jar package to open the image when it is not loaded.

The cause of this problem is mostly because of the path problem, often in the project to run normally, but once packaged into a jar can not be normal display, the following summarizes the solution to this problem:

One, through the use of external resource files to solve

After you have packaged the project to build the jar, create a new folder, put all the picture files and build jar files that are used in the project in the directory, as long as the code has the correct reference path to the picture file, such as: ImageIcon image=new ImageIcon ( Swingresourcemanager.getimage ("Icons/logo.gif"));

In this way, the picture file and the class file are not placed in the package, the class file is in the package, and the picture file is in the project root directory.

Ii. addressing through the use of internal resource documents

Put the picture files used in the project used in the package, packaging the picture file into the jar file at the same time. This way you should use one of the following in your specific code:

ImageIcon image = New ImageIcon (This.class.getResource ("icons/logo.gif"));

In the face of this way detailed description:

When you write a graphical interface program, you are sure to use a variety of picture resources. So, when you publish your program, how are these image resources released? The easiest way to do this is to package the image resources together with your class file as an executable jar file, so that you can just post a jar file that contains all the content, and users just double-click the jar file to run the program. If you pack your pictures in a jar file, how do you access the pictures when you write the program? What are the paths to these pictures? You can use the GetResource () method to solve this problem. Here are some of the 2 most common scenarios:

1, you package all resources for Andy.jar, your class is in a package: package andycpp; Your picture resources are all in the Images folder, and the Images folder is in the Andycpp package. In this case, the final path is:

Andy.jar

|__andycpp

|__ Guitest.class

|__ Images

|__ snap1.jpg

Because the picture folder and the classes that reference him are 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 you need to be aware of here is the problem of capitalization. The inside of the jar file is sensitive to case, so if the picture itself is called snap1.jpg, you write the path as images/snap1.jpg, the result will not find the picture. Also note that each class (note, not the object) has a getresource () method, but you can't use it casually, you can only use the GetResource () method of the class that is at the same level as the picture folder. For example, in the previous illustration, the Guitest class and the images are at the same level, so the GetResource () method of the class is used.

2, in another case, if you have a lot of classes, and the package structure is very complex, then you are not likely to put pictures and classes on the same level, but should put the picture to the outermost layer, so that all classes through the absolute path to access the picture:

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

Do you see the difference from the code above? The difference is very subtle, just in front of "images" plus a backslash "/", this backslash represents the root directory, no backslash is the relative path ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.