Set Icon Image in Jar file - java

The following code works fine when running on NetBeans.
this.getFrame().setIconImage(Toolkit.getDefaultToolkit().getImage("PlagiaLyzerIcon.png"));
However, once it was built into Jar file, the icon was gone.
Anyone has idea what's the problem? I realized I've to put the icon image on the root directory, however, after compiling into JAR, the icon gone.
Thanks for any help ...
Hi everyone, the problem was solved with the following code,
this.getFrame().setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("plagialyzer/resources/PlagiaLyzerIcon.png")));
It works once compiled into jar file.

Use
this.getFrame().setIconImage(
new imageIcon(getClass().getClassLoader().getResource("PlagiaLyzerIcon.png"))
);
instead.
Note:
this line only works if the images are in the root of the jar file. If not, you have to specify the folder on the string:
getResource("yourfolder/PlagiaLyzerIcon.png")

That is because the Netbeans IDE has a different classpath, than when running the jar-file stand-alone (without Ant).
Assume your Netbeans project is at location /project/:
The classpath is: /project/build/classes/ and the project root /project/. If your icons are stored in: /project/myicons/, then they are part of the classpath, since /project/ is too. But when you build your project, only files in /project/build/classes/ will eventually end up in the jar-file, these files are "build" from /projcet/src/.
Solution:
Move your icons into a source-package: /project/src/myicons/
Or, add the /project/myicons/ folder to your sources (right-click your project -> Properties -> Sources -> add your folder there)

Thanks a lot...
Here the code that works for me!!!!
BufferedImage image = null;
try {
image = ImageIO.read(getClass().getClassLoader().getResource("images/frame.png"));
} catch (IOException e) {
e.printStackTrace();
}
super.setIconImage(image);

Did you specify the build path to your icons in the options of Netbean before exporting the JAR? On Eclipse it's done by adding a source folder inside the Java Build Path as shown in this screenshot. Should be the same way on Netbeans?

Using NetBeans like myself, just put the logo (in my case mylogo.png) inside src/ directory in your project folder. Make sure size is 32 x 32 pixel to ensure best quality for icon. I couldn't manage to set .ico images as my JFrame icon but .png worked well.
By extending JFrame, I directly set my jar icon with:
Image getIcon()
{ return Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("mylogo.png")); }
setIconImage(getIcon());
After clean and build jar, the jar file icon still isn't changed (still the jar default icon) but when you execute the jar file the icon in title bar and taskbar will be your custom icon. You can change the jar file icon by creating shortcuts and change icon for that shortcut. Best resolution for shortcut icon is 512 x 512 pixel .ico

I use this code, but it only changes the little icon that you have top left on your windows and the icon that you have at the button on the bar where you can see which app is open.
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
constuctor of my class extends JFrame{
BufferedImage image = null;
try {
image = ImageIO.read(new File("path/logo.png"));
} catch (IOException e) {
e.printStackTrace();
}
super.setIconImage(image);
}

Related

Images are loading in eclipse, but are not working in exported Jar

Title says it all, when I compile in eclipse it works fine, when I Export to a Jar file, they don't show up.
If you need anymore of the code, I will provide.
Image sprite = new ImageIcon("Sprites/Characters/spikeEnemy.png").getImage();
sprite = (e) ? new ImageIcon("Sprites/enemyProjectile.png").getImage() : new ImageIcon("Sprites/projectile.png").getImage();
(e: true for enemy, false for ally) sprite is declared earlier as (private Image sprite)
you need to use getClassLoader:
put the picture inside the src forlder
Image sprite = ImageIO.read(getClass().getClassLoader().getResource(path));
-> src /icons/icon.png
ImageIO.read(getClass().getClassLoader().getResource("icons/icon.png"));
this whay the image will be inside the executable jar
This is a build path issue.
Right click the project in eclipse -> add source folder. Place your images in there and update your references in your code accordingly.
I now got it to only work in JAR files, but it doesn't work in eclipse...
I accidently ran it like
sprite = new ImageIcon("Sprites/Characters/Player1.png").getImage();
And the sprites now show up in the jar, but there is no folder called "Sprites" anymore. So is it just a default thing you are suppose to put there?

Displaying images within a jar file

I know this question has been asked before, but I have gone over all the solutions I could find and cannot make any of them work for me. I have a program in Eclipse that I am trying to export to an executable jar file. It is the first time I have done this. When I execute the jar, my program images do not show up. So I did some research and found I need to load them as resources, but I cannot seem to make it work.
Here is the code I was using to load the images without the jar:
private void initComponents()
{
// create an enterprise icon and make it invisible
enterpriseIcon = new JLabel(new ImageIcon("res/enterprise1.png"));
enterpriseIcon.setVisible(false);
}
I have all my images in a folder named res in my root project directory, and I told Eclipse to put this folder in the build path.
When the images wouldn't show up when running the jar file, I tried the following:
enterpriseIcon = new JLabel(new ImageIcon(getClass().getResource("/res/enterprise1.png")));
and
enterpriseIcon = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/res/enterprise.png"))));
However, when either of these is run from within Eclipse, I get a null pointer exception. (I tried a few other things as well, but the above were the only solutions I thought I understood.)
Any help getting this to work would be appreciated.
Use Class#getResource to get the URL of your image inside the jar file
enterpriseIcon = new JLabel(new ImageIcon(getClass().getResource("/res/enterprise1.png")));
Try this. This works well for me :)
private void initComponents() {
try {
BufferedImage icon = ImageIO.read(getClass().getResource("/enterprise1.png"));
frame.setIconImage(icon);
} catch (IOException e) {
e.printStackTrace();
}
}
I was also facing the same issue till yesterday. I have got a fix for this. Let me share it with you.
In your Eclipse, go to your Project name.
Right your project name.
New -> Source file/folder
Name this Source file/folder as images
Now on your local machine go to the Eclipse workspace where your project
is physically present.
Copy ALL your images into the newly created
'images' folder right there only.
Now go back to Eclipse.
Right click on your Project -> Refresh
Now, wherever you are using images in your .java files , go to those lines and prefix the image location with images/
Example : XXX xxx = XXXX (example.jpg)
Change it with .... XXX xxx = XXXX (images/example.jpg)
BOOM !!
The images will be shown when you run the .jar file.
NOTE : Do not run the .jar file from cmd using java -jar AppName.jar
Instead, just double click the .jar file and you will be seeing the images.
If it works for you, kindly upvote. :)

ClassLoader.getResource() doesn't work for the icon files in a jar. The icons need to be present in the folder containing jar

I'm working on a Java program that uses icon files. The icons are stored in a folder Icons in /src. The code to access an icon is
image1 = new ImageIcon(Control.PrimaryGameFrame.class.getClassLoader().getResource("Icons/openFile.gif"));
The program runs properly in Eclipse. But when I export its runnable jar to a folder abc, then it needs the Icons folder to be present in abc. If I place the jar alone at a place, it doesn't load the icons. What changes are to be made to the code, for the jar to find the icon files in itself and not in the folder it is in?
Try this:
ImageIcon image1 = new ImageIcon(getClass.getResource("Icons/openFile.gif");
This works fine for me.

Image not loading in jar file

I am trying to fix this problem. Trying different solutions but nothing works. I am using NetBeans IDE. I created a project with this structure and files:
E:\java\project\ecadpb\src\ecadpb
The image files are in
E:\java\project\ecadpb\src\
I have specified working folder for my project in Netbeans as E:\java\project\ecadpb
I have created my image icons like this
new ImageIcon("device21.png");
In my source file, it works perfectly while running the project in Netbeans but images are not showing up when I build and run my JAR file separately. But the image files are inside the JAR.
I have also tried the answers for the same question asked previously.
URL imageUrl=getClass().getResource("device21.png");
new ImageIcon(imageUrl);
But it doesn't work in my case. I am building a JAR file for the first time. Can anyone help me with this!!
A simple way of doing this will be to add the image in your classpath or a directory in your classpath say img as shown below:
E:\java\project\ecadpb\src\main\java\img\device21.png
And then load your image from this location like this:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource("img/device21.png");
ImageIcon icon = new ImageIcon(resource);
Create a source folder called Images (ie if you're using eclipse, right-click your project -> new ->sourceFolder). Call it whatever you want, i called my Images. Put some images in it.
Now i had JLabels where i gave them ImageIcons. Look at the following code.
ImageIcon BPawn;
ImageIcon WPawn;
JLabel Label = new JLabel[8][8] //2D array of labels that gives each spot a position.
public void setPieces(){
//set up pawns in their respective positions.
BPawn = new ImageIcon("Images/BPawn.png", "BPawn");
WPawn = new ImageIcon("Images/WPawn.png", "WPawn");
for(int i=0;i<Label[0].length;i++){
Label[1][i].setIcon(BPawn);
Label[6][i].setIcon(WPawn);
}//end for
}//end setPieces.
There is a lot more in setPieces() method, but this glimpse is how you would reference the images in your source folder when you create an executable jar and want the images to show up.
I think answer could be one these suggestions here
I have used a similar approach,
a) Specify the package path to the image file name.
b) Make sure that the image file is not ommited by your build scripts and that it is present in your jar file.

Java SystemTray icon

There is a really simple tutorial on implementing a system tray icon.
The problem is, I can see the icon in tray if I run my app from eclipse, but I can't see it if I export and run a runnable jar file. I have other images in my app that work fine form the same folder.
The tray is working (left and right click on it) but doesn't show the image, like you can see in the image (the jar file on top, with eclipse on bottom):
Why? Thank you and sorry for my english!
EDIT: I finally found the solution the image need to be accessed whit:
Image img = Toolkit.getDefaultToolkit().getImage(MyClass.class.getResource("/images/asd.png"));
The problem is the way you include the image file. You will have to include the image in the JAR when you create it, and you will have to access the image in a different manner:
try {
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("wing16.png");
BufferedImage img = ImageIO.read(is);
}
catch (IOException e) {}
You can just used the img variable to set the image in the JAR.
Update:
Take all of your class files & images and go to command line:
jar -cvfm Test.jar Manifest.mft *.class image.png
Replace Manifest.mft with your manifest file name. Replace image.png with the image you want to show up (you can include more images if you need to)

Categories