Hopefully someone can help me with something that is probably very simple. I'm new to Java coding (2 weeks in), and using Eclipse IDE under Linux. I'm currently in the process of creating a JFrame application, everything so far is going well.
I have one little snag though - I have included a set of Icons and assigned them to a JLabel, and have them displayed. Upon exporting an Executable JAR, they are not in the JAR as a resource. If I open the JAR file, I can see the Images, in the ROOT of the JAR, not organized in their respective folders. (icons/, etc).
TL;DR - How do I import resources, in their folders, into a JAR as a resource.
public void drawCategoryIcons() {
for (int i = 0; i < aspCategories.length; i++) {
Icon pcIcon = new ImageIcon(getClass().getResource( "/icons/" + cats[i]));
aspCategories[i] = new JLabel("", JLabel.CENTER);
aspCategories[i].setIcon(pcIcon);
panel.add(aspCategories[i], "w 200, center");
}
}
If I RUN the project within Eclipse, everything works as it should. Exporting it, I get tons of errors.
You have probably created the icons directory as separate source folder in the eclipse project (it has an icon composed of a folder icon and a package icon then). Source folders are just logical elements during development (so you can separate modules of your application logically), they are not part of the exported application. Only everything inside source folders is exported.
Therefore a simple workaround might be to instead create a package "icons" in your other source folder (typically called "src"). Those packages will exist as sub folders after export.
Related
I have a Java Project in NetBeans 7.0.
I want to add some image to some label dynamically. The image will differ depending on the state of the program.
I put one such image, 'filling.jpg', in the 'resources' folder of my project.
I want to reach this file correctly (not by absolute or relative path, because that will cause problems when I build the jar file).
So I found this method:
ImageIcon fillingIcon = new ImageIcon(getClass().getClassLoader().getResource("filling.jpg"));
labelFontFilling.setIcon(fillingIcon);
It keeps give me java.lang.NullPointerException.
But I am sure that there is that image, because I can assign the image to the label from the NetBeans Properties menu for that label (but I don't want this, I want to add the image by Java code).
What am I doing wrong, and how can I get that image correctly?
This was a pain, using netBeans IDE 7.2.
You need to remember that Netbeans cleans up the Build folder whenever you rebuild, so
Add a resource folder to the src folder:
(project)
src
project package folder (contains .java files)
resources (whatever name you want)
images (optional subfolders)
After the clean/build this structure is propogated into the Build folder:
(project)
build
classes
project package folder (contains generated .class files)
resources (your resources)
images (your optional subfolders)
To access the resources:
dlabel = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("resources/images/logo.png")));
and:
if (common.readFile(getClass().getResourceAsStream("/resources/allwise.ini"), buf).equals("OK")) {
worked for me. Note that in one case there is a leading "/" and in the other there isn't.
So the root of the path to the resources is the "classes" folder within the build folder.
Double click on the executable jar file in the dist folder. The path to the resources still works.
I have a slightly different approach that might be useful/more beneficial to some.
Under your main project folder, create a resource folder. Your folder structure should look something like this.
Project Folder
build
dist
lib
nbproject
resources
src
Go to the properties of your project. You can do this by right clicking on your project in the Projects tab window and selecting Properties in the drop down menu.
Under categories on the left side, select Sources.
In Source Package Folders on the right side, add your resource folder using the Add Folder button. Once you click OK, you should see a Resources folder under your project.
You should now be able to pull resources using this line or similar approach:
MyClass.class.getResource("/main.jpg");
If you were to create a package called Images under the resources folder, you can retrieve the resource like this:
MyClass.class.getResource("/Images/main.jpg");
Thanks, Valter Henrique, with your tip i managed to realise, that i simply entered incorrect path to this image.
In one of my tries i use
String pathToImageSortBy = "resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
But correct way was use name of my project in path to resource
String pathToImageSortBy = "nameOfProject/resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
For me it worked like I had images in icons folder under src and I wrote below code.
new ImageIcon(getClass().getResource("/icons/rsz_measurment_01.png"));
I am busy with a java desktop application in intellij. I am struggling to get a image to display in a JLabel
Here is my current code (in a class extending JPanel):
icon = new ImageIcon(getClass().getResource("resources/icon.png"));
lblIcon.setIcon(icon);
Here is a picture of my project structure:
The image is in the resources directory and the screen in in the screens\jpanel.java directory
If I rememeber correctly, IntelliJ automatically adds the contents of the folder marked as "resources root" to the root of your compiled project.
getClass().getResource("...") expects a path relative to your classloader, thus you simply need to provide the name of your image in this case:
icon = new ImageIcon(getClass().getResource("/icon.png"));
lblIcon.setIcon(icon);
I really have a problem adding images to my java project, and before you ask, yes i have searched already and tried everything, but i just can't get it working.
Here is my problem:
At the moment i am using this code to get the images:
ImageIcon goldIcon = new ImageIcon("res/Gold_coin.png");
ImageIcon silverIcon = new ImageIcon("res/Silver_icon.png");
ImageIcon copperIcon = new ImageIcon("res/Copper_icon.png");
My project structure is as following:
I have one project folder with two sub folders.
Both sub folders are specified as source folders, one is the "src" folder and the other one i named "res". In the "src" folder i have one package with all classes in it. In the "res" folder i have all the images saved.
Now the strange thing is, the "Gold_icon" DOES work, but both silver and copper do NOT. I am using eclipse luna and if someone could give me a step by step instruction how to add an image would be really nice.
Because all i find is always "add to resource" , "add it to resources folder" and honestly, i tried creatig a new folder, i copied it to the "src" folder, i tried every possible call, from ("res/Gold_coin.png") over ("/Gold_coin.png") to
("Gold_coin.png") and ("/res/Gold_coin.png")
I refreshed the project, the folders, the package, the classes, i restarted eclipse but nothing helps
I just don't get it..
Please help :(
If you need the information what i want to do with this images afterwards, i am adding them together into a JPanel with flowlayout which i write into a JTable cell with a cellrenderer, which is everything working with the gold icon, but not the other two. And it also does not work to remove the gold icon (because i thought maybe for whatever reason only the first icon works..) but then nothing is displayed
You should use getResource to load images or whatever from resource folder.
For example:
String pathToImage = "res/Gold_coin.png";
ImageIcon myIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImage));
or with all project path: nameOfProject/res/Gold_coin.png.
First of all, I am aware of Stack Overflow (and any competent forum-like website) policy of "search first, ask last", and, doing my homework, I searched various sources to find a solution to my issue. That said, I, failing to find any suitable answers, was left no choice but to ask this problem personally.
I have somewhat moderate programming skills, especially regarding the Java language. I am working on this 2D game with the default Java SE JDK. More specifically JDK 7u4. In this project, we have a class that manages most I/O operations. One of its methods returns the path to a file:
public static URL load(String resource) {
return ZM.class.getResource(resource);
}
Now, this method works fine when running the project on Netbeans (version 7.1). However, when building and cleaning the project, the resulting .jar file does not seem to agree with its creator. When running the .jar on command line, the JVM caught a NullPointerException. It seemed that the file was not being able to be read inside the .jar. Following my programmers instinct, I started debugging the project. My first attempt was to check whether the load method was the faulty member. I ran some tests and obtained a couple of interesting results:
When running the application on Netbeans and with "ZM.class" as the methods argument, it returned:
/D:/Projects/GeometryZombiesMayhem/build/classes/geometryzombiesmayhem/ZM.class
But when running it from the .jar file, it returned:
file:/D:/Projects/GeometryZombiesMayhem/dist/GeometryZombiesMayhem.jar!/geometryzombiesmayhem/ZM.class
Naturally, I tried removing the initial file: string from it. No effect. Then I tried taking the exclamation mark from [...].jar![...]. Again, nothing. I tried removing all the possible permutations from the path. No luck.
Testing the method against the very own .jar file worked okay. Now, when I try to access the inside of the file, it doesn't let me. On earlier versions of this project it worked just fine. I am not really sure of what is going on. Any help is welcome.
Thank you in advance,
Renato
When loading resources from a jar file, I've always used a classLoader. Everything seems to work the same whether you run from within the IDE, launch the executable jar file or run the program from a web site using JNLP.
Try loading the resource this way instead:
try {
ClassLoader cl = ZM.getClass().getClassLoader();
ImageIcon img = new ImageIcon(cl.getResource("images/programIcon.jpg"));
// do stuff with img.
}
catch(Exception failed) {
System.out.println(failed);
}
One more suggestion - you should create a separate folder for resources. In my example above, images is a folder inside of my src folder. This way it will automatically become part of the jar when I build it, but I am keeping resources separate from source code.
I suppose your problem is in loading an image from your jar file.
Here is how i do it
URL imageurl = Myclassanme.class.getResource("/test/Ergophobia.jpg");
Image myPicture = Toolkit.getDefaultToolkit().getImage(imageurl);
JLabel piclabel = new JLabel(new ImageIcon( myPicture ));
piclabel.setBounds(0,0,myPicture.getWidth(null),myPicture.getHeight(null));
This way I can get the Ergophobia.jpg file inside 'test' package.
I have a Java Project in NetBeans 7.0.
I want to add some image to some label dynamically. The image will differ depending on the state of the program.
I put one such image, 'filling.jpg', in the 'resources' folder of my project.
I want to reach this file correctly (not by absolute or relative path, because that will cause problems when I build the jar file).
So I found this method:
ImageIcon fillingIcon = new ImageIcon(getClass().getClassLoader().getResource("filling.jpg"));
labelFontFilling.setIcon(fillingIcon);
It keeps give me java.lang.NullPointerException.
But I am sure that there is that image, because I can assign the image to the label from the NetBeans Properties menu for that label (but I don't want this, I want to add the image by Java code).
What am I doing wrong, and how can I get that image correctly?
This was a pain, using netBeans IDE 7.2.
You need to remember that Netbeans cleans up the Build folder whenever you rebuild, so
Add a resource folder to the src folder:
(project)
src
project package folder (contains .java files)
resources (whatever name you want)
images (optional subfolders)
After the clean/build this structure is propogated into the Build folder:
(project)
build
classes
project package folder (contains generated .class files)
resources (your resources)
images (your optional subfolders)
To access the resources:
dlabel = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("resources/images/logo.png")));
and:
if (common.readFile(getClass().getResourceAsStream("/resources/allwise.ini"), buf).equals("OK")) {
worked for me. Note that in one case there is a leading "/" and in the other there isn't.
So the root of the path to the resources is the "classes" folder within the build folder.
Double click on the executable jar file in the dist folder. The path to the resources still works.
I have a slightly different approach that might be useful/more beneficial to some.
Under your main project folder, create a resource folder. Your folder structure should look something like this.
Project Folder
build
dist
lib
nbproject
resources
src
Go to the properties of your project. You can do this by right clicking on your project in the Projects tab window and selecting Properties in the drop down menu.
Under categories on the left side, select Sources.
In Source Package Folders on the right side, add your resource folder using the Add Folder button. Once you click OK, you should see a Resources folder under your project.
You should now be able to pull resources using this line or similar approach:
MyClass.class.getResource("/main.jpg");
If you were to create a package called Images under the resources folder, you can retrieve the resource like this:
MyClass.class.getResource("/Images/main.jpg");
Thanks, Valter Henrique, with your tip i managed to realise, that i simply entered incorrect path to this image.
In one of my tries i use
String pathToImageSortBy = "resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
But correct way was use name of my project in path to resource
String pathToImageSortBy = "nameOfProject/resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
For me it worked like I had images in icons folder under src and I wrote below code.
new ImageIcon(getClass().getResource("/icons/rsz_measurment_01.png"));