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();
}
}
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.
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.
In order to make it work, follow these steps :
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With