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)
Related
When running a Java app from eclipse my ImageIcon shows up just fine.
But after creating a jar the path to the image obviously gets screwed up.
Is there a way to extract an image from the jar at runtime so I can then open it up? Or, is there a better way to do this?
I'd like to distribute a single jar file if possible.
To create an ImageIcon from an image file within the same jars your code is loaded:
new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg"))
Class.getResource returns a URL of a resource (or null!). ImageIcon has a constructors that load from a URL.
To construct a URL for a resource in a jar not on your "classpath", see the documentation for java.net.JarURLConnection.
You can try something like:
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/images/image.jpg");
In your JAR file, you might have a directory structure of:
MyJAR.jar
- com (class files in here)
- images
----image.jpg
This is working for me to load and set the content pane background image:
jar (or build path) contains:
- com
- img
---- bg.png
java contains:
JFrame f = new JFrame("Testing load resource from jar");
try {
BufferedImage bg = ImageIO.read(getClass().getResource("/img/bg.png"));
f.setContentPane(new ImagePanel(bg));
} catch (IOException e) {
e.printStackTrace();
}
Tested and working in both jar and unjarred (is that the technical term) execution.
BTW getClass().getClassLoader().getResourceAsStream("/img/bg.png") - which I tried first - returned me a null InputStream.
In netbeans 8.1 what I've done is to include the folder of icons and other images called Resources inside the src folder in the project file. So whenever i build Jar file the folder is included there.The file tree should be like this:
src (Java files in source packges are here)
** PACKAGE YOU NAMED IN PROJECT**
file.java
Resources
image.jpg
The code should be like:
jToggleButton1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("/resources/image.jpg")));
Load image in from Jar file during run time is the same as loading image when executed from IDE e.g netbeans the difference is that when loading image from JAR file the path must be correct and its case sensitive (very important).
This works for me
image1 = new javax.swing.ImageIcon(getClass().getResource("/Pictures/firstgame/habitat1.jpg"));
img = image1.getImage().getScaledInstance(lblhabitat1.getWidth(), lblhabitat1.getHeight(), Image.SCALE_SMOOTH);
lblhabitat1.setIcon(new ImageIcon(img));
if p in "/Pictures/firstgame/habitat1.jpg" is in lower case it wont work. check spaces, cases and spelling
When running a Java app from eclipse my ImageIcon shows up just fine.
But after creating a jar the path to the image obviously gets screwed up.
Is there a way to extract an image from the jar at runtime so I can then open it up? Or, is there a better way to do this?
I'd like to distribute a single jar file if possible.
To create an ImageIcon from an image file within the same jars your code is loaded:
new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg"))
Class.getResource returns a URL of a resource (or null!). ImageIcon has a constructors that load from a URL.
To construct a URL for a resource in a jar not on your "classpath", see the documentation for java.net.JarURLConnection.
You can try something like:
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/images/image.jpg");
In your JAR file, you might have a directory structure of:
MyJAR.jar
- com (class files in here)
- images
----image.jpg
This is working for me to load and set the content pane background image:
jar (or build path) contains:
- com
- img
---- bg.png
java contains:
JFrame f = new JFrame("Testing load resource from jar");
try {
BufferedImage bg = ImageIO.read(getClass().getResource("/img/bg.png"));
f.setContentPane(new ImagePanel(bg));
} catch (IOException e) {
e.printStackTrace();
}
Tested and working in both jar and unjarred (is that the technical term) execution.
BTW getClass().getClassLoader().getResourceAsStream("/img/bg.png") - which I tried first - returned me a null InputStream.
In netbeans 8.1 what I've done is to include the folder of icons and other images called Resources inside the src folder in the project file. So whenever i build Jar file the folder is included there.The file tree should be like this:
src (Java files in source packges are here)
** PACKAGE YOU NAMED IN PROJECT**
file.java
Resources
image.jpg
The code should be like:
jToggleButton1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("/resources/image.jpg")));
Load image in from Jar file during run time is the same as loading image when executed from IDE e.g netbeans the difference is that when loading image from JAR file the path must be correct and its case sensitive (very important).
This works for me
image1 = new javax.swing.ImageIcon(getClass().getResource("/Pictures/firstgame/habitat1.jpg"));
img = image1.getImage().getScaledInstance(lblhabitat1.getWidth(), lblhabitat1.getHeight(), Image.SCALE_SMOOTH);
lblhabitat1.setIcon(new ImageIcon(img));
if p in "/Pictures/firstgame/habitat1.jpg" is in lower case it wont work. check spaces, cases and spelling
When running a Java app from eclipse my ImageIcon shows up just fine.
But after creating a jar the path to the image obviously gets screwed up.
Is there a way to extract an image from the jar at runtime so I can then open it up? Or, is there a better way to do this?
I'd like to distribute a single jar file if possible.
To create an ImageIcon from an image file within the same jars your code is loaded:
new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg"))
Class.getResource returns a URL of a resource (or null!). ImageIcon has a constructors that load from a URL.
To construct a URL for a resource in a jar not on your "classpath", see the documentation for java.net.JarURLConnection.
You can try something like:
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/images/image.jpg");
In your JAR file, you might have a directory structure of:
MyJAR.jar
- com (class files in here)
- images
----image.jpg
This is working for me to load and set the content pane background image:
jar (or build path) contains:
- com
- img
---- bg.png
java contains:
JFrame f = new JFrame("Testing load resource from jar");
try {
BufferedImage bg = ImageIO.read(getClass().getResource("/img/bg.png"));
f.setContentPane(new ImagePanel(bg));
} catch (IOException e) {
e.printStackTrace();
}
Tested and working in both jar and unjarred (is that the technical term) execution.
BTW getClass().getClassLoader().getResourceAsStream("/img/bg.png") - which I tried first - returned me a null InputStream.
In netbeans 8.1 what I've done is to include the folder of icons and other images called Resources inside the src folder in the project file. So whenever i build Jar file the folder is included there.The file tree should be like this:
src (Java files in source packges are here)
** PACKAGE YOU NAMED IN PROJECT**
file.java
Resources
image.jpg
The code should be like:
jToggleButton1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("/resources/image.jpg")));
Load image in from Jar file during run time is the same as loading image when executed from IDE e.g netbeans the difference is that when loading image from JAR file the path must be correct and its case sensitive (very important).
This works for me
image1 = new javax.swing.ImageIcon(getClass().getResource("/Pictures/firstgame/habitat1.jpg"));
img = image1.getImage().getScaledInstance(lblhabitat1.getWidth(), lblhabitat1.getHeight(), Image.SCALE_SMOOTH);
lblhabitat1.setIcon(new ImageIcon(img));
if p in "/Pictures/firstgame/habitat1.jpg" is in lower case it wont work. check spaces, cases and spelling
I have been able to do this before with simpler programs, I just exported the project to a runnable .jar and it worked fine.
Every time my friend tries to open the program it keeps throwing the error messages that I put in place saying that, in general, it can't load the Images that I have added. I have put my images in a separate folder that I turned into a build path or whatever.
In the program I reference the images like so:
BufferedImage img= ImageIO.read(getClass().getResourceAsStream("/img.png"));
I am using eclipse if that helps. Should I just take the images out of the 'res' folder and just put it in the original 'src' folder?
If anyone has any advice it will be greatly appreciated! Thanks in advance!
The image must be inside the src folder.
Put it inside the src folder, refresh the project in Eclipse and use
BufferedImage img = ImageIO.read(getClass().getResourceAsStream("/img.png"));
If the image is inside a package, use
BufferedImage img = ImageIO.read(getClass().getResourceAsStream("/PACKAGE/img.png"));
if it still doesn't work, try using getResource instead of getResourceAsStream
BufferedImage img = ImageIO.read(getClass().getResource("/img.png"));
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);
}