How to enable icon when I export project to jar file

I developed a desktop application. The problem is that when exporting the application to a jar file, the icon does not appear in the application. When I launch it from Eclipse, all the icons are displayed there.

An example from my project:

package net.ebank.gui; import java.awt.*; import javax.swing.*; public class EBank extends JFrame { protected Start s; public EBank() { setTitle("Welcome To EBank"); setBackground(Color.white); Image img = new ImageIcon("../EBank/res/bank.jpg").getImage(); this.setIconImage(img); /*"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"*/ setVisible(false); setSize(1350,700); setDefaultCloseOperation(this.EXIT_ON_CLOSE); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { // TODO Auto-generated catch block e.printStackTrace(); } s= new Start(this); s.setLocation(getWidth()/2, getHeight()/4); } public static void main(String[] args){ new EBank(); } } 
+8
java jar swing embedded-resource
source share
3 answers

Use Class#getResource() instead of passing the relative path to the ImageIcon constructor.

Also make sure that the image file is actually part of the generated JAR.

+6
source share

To do the job, follow these steps:

  • Right-click the project in the project explorer tree.
  • Go to New → Source Folder, and then specify any name for the Source Folder.
  • Now manually add your materials to this source folder that you created, for example, if you want to add images, then create a new folder by manually visiting this source folder through the file system.
  • Name this new folder as images and copy the images to this folder.
  • Now go back to your Eclipse IDE and update your project using Project Explorer by right-clicking on your project, here you can see your added content after the update.
  • Now, to access, say, any image, you will use.

     getClass().getResource("/images/yourImageName.extension"); 

which will return a single URL object. Remember the first slash , in this case, since everything that is inside your source folder is accessible using this, in simpler terms. Now, when you start the project, the contents of this source folder will be automatically added to the bin folder, and when you create the Runnable Jar , the material inside your source folder may be available as it is.

In this regard, I tried to explain that with images, how to add images to an Eclipse project

+5
source share

You can either fix the layout of your JAR file, which was just to arrange the folders accordingly, as they are in Eclipse, or just do as Matt Ball said and use Class#getResource or Class#getResourceAsStream .

+4
source share

All Articles