How can I include my icon when I export my project

2019-04-24 10:13发布

问题:

I have developed an desktop application. The problem is when I export the application to jar file the icon isn't shown in the app. When I run it from Eclipse all icons are shown 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();
    }

}

回答1:

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

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



回答2:

In order to make it work, follow these steps :

  • Right-Click your Project in Project Explorer Tree.
  • Go to New -> Source Folder and then provide any Name to the Source Folder.
  • Now manually add your stuff to this Source Folder so created by you, like if you want to add images then make a New Folder, by manually visiting this Source Folder through File System.
  • Name this New Folder as images and copy your images to this Folder.
  • Now go back to your Eclipse IDE and Refresh your Project from the Project Explorer, by Right Clicking your Project, here you be able to see your added content now after refreshing.
  • Now in order to access, say any image, you will use.

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

which will return one URL object. Do remember the first forward slash, in this case, since whatever is inside your Source Folder is accessed with the help of this, in simpler terms. Now when you will Run your project, the content of this Source Folder will be automatically added to the bin folder and when you will create a Runnable Jar, then the stuff inside your Source Folder can be accessed as it is.

On this link, I had tried to explain that with images, how to add Images to your Eclipse Project



回答3:

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