How to include resource files in the .jar?
Hi there,
I have created a .jar using IntelliJ IDEA by Build 'xxxPackage' artifact. And I have got xxxPackage.jar package. However, when I try to start it by double-click, the program starts without showing any icons nor images which should be included as resources.
My project directory is like
xxxPackage
|______________META-INF
|______________src
|______________res
|______________xxx
in which, the 'res' contains all the icon and images in .png files, and xxx folder contains my .java codes. And I use the icon as:
Icon openIcon = new ImageIcon("src/res/fullscreen_16.png");
If I start the program using IDE directly, these icons and images show correctly.
I am wondering how to set the 'Project Structure' in oder to read those resources files when I start the xxxPackage.jar.
Thanks a lot.
Please sign in to leave a comment.
Hello,
Please attach resultin *.jar file.
Denis
Hi Denis,
Here it is the .jar attached, the resources are already in the .jar, but it is still not showing any icons.
And in the codes I wrote
Icon openIcon = new ImageIcon("src/res/fullscreen_16.png");
My project directory is like
xxxPackage
|______________META-INF
|
|______________src
|______________res
| |______________xxx.png
|
|______________xxx
|______________xxx.java
The .jar directory is like
My project directory is like
xxxPackage.jar
|______________META-INF
|
|______________xxx
| |______________xxx.class
|
|______________res
|______________xxx.png
Attachment(s):
WordsCracker.jar
It's reasonable to not find the resource as there is no resource by the given path. Your resources are accessible from the 'res' entry that is located at classpath, i.e. you can perform call like 'Icon openIcon = new ImageIcon("res/fullscreen_16.png");'
Denis
Do you mean that I need to add the aboslute path of /res in the CLASSPATH environment variable?
And build the project using modification of
'Icon openIcon = new ImageIcon("res/fullscreen_16.png");'
rather than
'Icon openIcon = new ImageIcon("src/res/fullscreen_16.png");'
Am I right?
Cheers.
No, you don't need to perform any additional changes at your environment. Just reference the resource without 'src' at your code, i.e. use 'new ImageIcon("res/fullscreen_16.png")' instead of 'new ImageIcon("src/res/fullscreen_16.png")' and everything should be ok.
Denis
Thanks. Maybe I did misunderstand you but my project directory structured like this:
My project directory is like
xxxPackage
|______________META-INF
|
|______________src
|______________res
| |______________xxx.png
|
|______________xxx
|______________xxx.java
and if I use
'new ImageIcon("res/fullscreen_16.png")'
no icon will show up because the compiler complains ''can't find file' error using the IntelliJ IDEA IDE to build.
If I change the project structure into
xxxPackage
|______________META-INF
|
|______________res
| |______________xxx.png
|
|______________src
|
|______________xxx
|______________xxx.java
Then I can use
'new ImageIcon("res/fullscreen_16.png")'
under IDE and the icon shows in final build. However, if I build the .jar like this, and the problem still there. Double-click .jar and no icons at all. Any ideas?
Looks like your module setup is inconsistent to your jar structure.
Basically you should do the following (assuming the last mentioned directory structure):
Then you should check that your *.jar file contains files from 'xxxPackage/res' at the root entry and reference them by simple name like 'new ImageIcon("fullscreen_16.png")'
Denis