Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include my icon when I export my project to a jar file

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();
    }

}
like image 759
Ali Taha Avatar asked Dec 19 '12 02:12

Ali Taha


People also ask

How do you put an icon in a jar file?

jar and then you can change it: Right button >> Properties >> Change Icon. p.s. The icon file should be a . ico, so you can use many site for conversion in . ico extension if your image has got another one.

How do I export a jar file?

To export your project, right-click it and select Export. Select Java > Runnable JAR file as the export destination and click Next. On the next page, specify the name and path of the JAR file to create and select the Launch configuration that includes the project name and the name of the test class.


2 Answers

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

like image 127
nIcE cOw Avatar answered Oct 21 '22 08:10

nIcE cOw


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.

like image 29
Matt Ball Avatar answered Oct 21 '22 08:10

Matt Ball