In Eclipse, package a project containing image resources into a jar file,
Preface:
Recently I learned GUI programming and UDP protocol, and I was eager to develop a chat software in the LAN. It was still quite smooth in the early stage, and it was difficult to package the entire project into a jar file.
The icon you set is missing, but there is no default icon. This indicates that the image is not exported to the jar file. After some searches, I finally found a solution.
Tools and materials:
1. Eclipse
2. Images and projects using them
Solution:
First, click the Eclipse window -- preference -- Java -- Build Path (Windows -- Performance -- Java -- Build Path) and change it to the following (this should be the default)
Below is the position of my picture in the project
This is the code I called. It is written in UIUtil. java under the util package and can be called wherever necessary.
// Modify the public static void setFrameIamge (JFrame jf) {Toolkit tk = Toolkit. getdefatooltoolkit (); Image I = tk. getImage (jf. getClass (). getResource ("/resource/user.jpg"); // note that "/" jf. setIconImage (I );}
Then, package the project, click "File -- Export", and select "executable JAR Files"
Select the project to be exported, that is, to select the "STARTUP configuration" (note that its naming format is "class name-Project Name". For example, if my project name is ChatOne, the main function is located in the LoginOne class), Click Browse to view the hanging export directory and name the jar file, select the second library processing, and click Finish.
Open "run" with Win + R, Enter cmd, switch to the desktop (because I export the file to the desktop, it is actually to switch to the directory where the jar file is located), and run the jar File
C:\Users\Administrator>cd DesktopC:\Users\Administrator\Desktop>java -jar Chat_One.jar
You can see that the program has an icon.
Conclusion:
The above is the process of exporting a project containing image resources as a jar file. I am a little bit white and the level is limited. If there is any error above, please point it out.