A solution to the problem that the images of jar packages that can be exported to java programs in Eclipse cannot be displayed

Source: Internet
Author: User

A solution to the problem that the images of jar packages that can be exported to java programs in Eclipse cannot be displayed

Note: Everything works normally when running the java program in eclipse. However, when exporting the jar package, the image cannot be displayed, this is probably caused by various configuration and Path Problems of the java program. Later, I found a solution to this problem, which is the same as that of the java program:

The directory structure of the Java project is as follows:

 

Put the class in the package: package accpedu; (that is, it is actually under the bin/accpedu folder above)

When you reference an image using the following method, the image can be displayed normally in eclipse:

ImageIcon image1 = new ImageIcon ();

ImageIcon image2 = new ImageIcon ();

ImageIcon image3 = new ImageIcon ();

ImageIcon image4 = new ImageIcon ();

ImageIcon image5 = new ImageIcon ();

Image1 = new ImageIcon ("images/butffler.jpg ");

Image2 = new ImageIcon ("images/username.gif ");

Image3 = new ImageIcon ("images/password.gif ");

Image4 = new ImageIcon ("images/warning.gif ");

Image5 = new ImageIcon ("images/role2.jpg ");

 

 

However, after exporting the jar package school2.jar and executing it, the image is not displayed:


 

After some analysis and debugging, I found a solution to change the code of the referenced image to the following method:

// You can find the image path in eclipse or in the exported jar package.

// The image must be placed under the class directory

Java.net. URL imgURL1 = LoginFrame. class. getResource ("butffler.jpg ");

Image1 = new ImageIcon (imgURL1 );

Java.net. URL imgURL2 = LoginFrame. class. getResource ("username.gif ");

Image2 = new ImageIcon (imgURL2 );

Java.net. URL imgURL3 = LoginFrame. class. getResource ("password.gif ");

Image3 = new ImageIcon (imgURL3 );

Java.net. URL imgURL4 = LoginFrame. class. getResource ("warning.gif ");

Image4 = new ImageIcon (imgURL4 );

Java.net. URL imgURL5 = LoginFrame. class. getResource ("role2.jpg ");

Image5 = new ImageIcon (imgURL5 );

 

Put it in the same directory of the class, that is, put it under the directory accpedu, otherwise the image path will not be found; in this way, you need to change the images folder to accpedu and compress it to accpedu. jar package. The accpedu file contains a direct image file without redundant folders. This is because the class is placed in the package accpedu;, and then you can import accpedu. jar to the project, as follows:



 

After the accpedu. jar package is imported using such a naming rule (package accpedu;), the image is automatically imported to the same directory as the class:

Then export the executable jar package school. jar of the java program:


 

After extracting the school. jar package, the image is indeed imported to the same directory as the class:


 

Note: If the screenshot is written as butffler1.jpg, the following problem may occur. Not only can the image not be displayed, but more seriously, the entire logon box cannot be displayed completely. Note this:


 

At this point, the above is a way to solve the problem that the images of executable jar packages exported to java programs by eclipse cannot be displayed!


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.