Description: When running java programs in Eclipse, everything is fine, but when the jar is exported it is not shown, which is an estimate Java The various configuration and path problems of the program, and later found a solution for learning about this problem Java application of the shoes with reference:
the directory structure under the Java project is as follows:
which class classes placed in Packages: Packageaccpedu; (that is, the actual bin/accpedu folder below)
When you refer to a picture by using the following method, the image is 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 , the School2.jar is executed, but the image is not displayed:
After some analysis and debugging, a workaround is found to modify the code that refers to the picture to the following way:
This way you can find the image path under Eclipse, or you can find the picture path in the exported jar package
// The picture must be placed below 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);
Then you need to import the jar of a picturepackage, as this method must put the picture file (such asbutffler.jpg) placed inclassin the same directory, which is placed in the directoryaccpeduyou will not find the path to the image;Imagesfolder renamed toaccpeduand compress toAccpedu.jarpackage, whereaccpeduThe file is directly in the picture file, no unnecessary folders, this is becauseclass classes placed in Packages: Packageaccpedu; in; Then you can importAccpedu.jarto the project, as follows:
Through such a naming convention (packageaccpedu; After importing the Accpedu.jar package, its pictures are automatically imported into the same class directory:
Then export the Java program's executable jar package school.jar:
After unpacking the School.jar package, the picture is actually imported into the same class directory:
Note: The command rules for the jar package of the picture must match the structure of class in the package , so that the image that needs to be imported is correctly directed to the class the same directory; In addition, in this way to display the picture, you must ensure that the picture is directed to the class directory, or the program will run a problem, the fake file name is wrong Butffler.jpg written butffler1. jpg, The following problem will occur, not only the picture is not displayed, What is more serious is that the entire login box does not appear complete, which is important to note:
Here, here is a way to resolve the image of an executable jar package that resolves the Eclipse export java program !
A workaround for exporting a Java program executable jar package picture under Eclipse does not display a problem