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"));
Related
I am creating an eclipse workspace starting by a java project (not written by me).
I am facing problems with the following method:
public static URL getURL(String fileName) {
URLClassLoader urlLoader = (URLClassLoader) getInstance().getClass()
.getClassLoader();
URL fileLocation = urlLoader.findResource(fileName);
return fileLocation;
since the findResource doesn't find the JPG resource (filename = "icons/INIT.JPG").
Looking on urlLoader.getUrl, I noticed the class aims only to jar files. Adding the folder icon to the Project->Libraries under eclipse I managed to let findResources look into the icon folder: nevertheless, the image is not a jar file and so it isn't considered.
Honestly, I don't get the point of using this process to load an image, but I cannot change the code and I was hoping in a solution within Eclipse project setup.
Thanks in advance
Based on the answers to my questions in the original comment, there are some facts:
You cannot change the code, and it looks like it's retrieving the AppClassLoader.
Even if you cast it into URLClassLoader, it's still an instance of an AppClassLoader, so it will look for the contents of the classpath and all JAR/ZIP files in JAVA_HOME\lib\ext.
You said that the project is guaranteed to work without to move the file anywhere, so there's only one option: add the file that you want to retrieve with the ClassLoader to the classpath.
Right click on the project, select Build Path and choose Configure Build Path.
Click on Source > Add Folder... and add the folder where the resources that you want to take are.
PD: If you add the folder as Class Folder in the Libraries tab, the JPG image won't be recognised by the AppClassLoader.
I am trying to display an icon in my GUI by using a relative path, as in "display image from resources/image.png". I have tried a million different ways to express this, but nothing works. This makes me think it's a problem with my IntelliJ IDEA settings or project structure. I have set up the "resources" folder as a "resources folder". I don't know what else it expects me to do.
How can I load an icon from a file using a relative path in a Java project within IntelliJ IDEA?
My project structure:
src/main/java/ <-- set as "sources" in IntelliJ
src/main/java/ui/ <-- contains classes for my GUI
src/main/resources/ <-- set as "resources" in IntelliJ. Contains images.
Edit: Able to use relative path to confirm that file is found, not able to load it as icon.
String path = "src/main/resources/image.png";
System.out.println(new File(path).exists()); <-- true
I've encountered this issue many times and what worked for me was using InputStream
InputStream is = Main.class.getClassLoader().getResourceAsStream("name_of_file.png");
Using InputStream will allow you read from various file types. Now to load in the icon you can do
Icon icon = new ImageIcon(ImageIO.read(is));
Resources are on classpath, not on filesystem path - which is taken relative from running directory, which is project directory when you are running it from idea. Usually you will distribute your aplication as jar, and this it is better to load resources from classpath. In zour case - from root directory
my problem is that when i build a jar with intellij 13.1.1 and include images to the jar, the path to the images are broken.
i get a nullpointerexception.
when i add the resource folder outside the jar and link the path as you can see in the code example it works. But my aim is to include the resources folder to the jar file, so that i only have one file.
i run ubuntu.
my project structure:
res -> images -> button.gif
src -> programm -> mainclass.java
code
image = Toolkit.getDefaultToolkit().createImage("res/images/animation.gif");
this.setContentPane(new JLabel(new ImageIcon( image)));
If you want to load resources from within an exported .jar file, you cannot statically reference their location like you do when they are outside the jar. Depending on the path of the gif use either one of these:
final String internalImagePath = "res/images/animation.gif";
//example one
Toolkit.getDefaultToolkit().createImage(this.getClass().getClassLoader().getResource(internalImagePath));
//example two
Toolkit.getDefaultToolkit().createImage(MainClass.class.getResource(internalImagePath));
If the images are relative to the class containing this code, use the first loader example. If the images are relative to some class other than the one in which this code exists, use the second example.
Hope this helps. Cheers.
I have created a project, inside the project there is a folder named Source Packages, which contains my packages.
I need images, and I want to copy them in a folder inside the project (or better inside the Source packages), so that when I create the .jar the image folder will be inside the jar file.
How can I do that in NetBean?
Edit: I still can't figure it out, this is the code:
Image star;
InputStream stream = getClass().getResourceAsStream("images/star.png");
star= ImageIO.read(stream);
It doesn't work, I get the error "IllegalArgumentException input==null"
the folder "images" is inside the folder of the project "Game", if I try with this code:
Image star;
InputStream stream = getClass().getResourceAsStream("images/star.png");
star= Toolkit.getDefaultToolkit().getImage("images/star.png");
it works, what am I doing wrong with the InputStream?
You can just copy/paste your images into your package or into the folder which is represented by the package. When you want to access the images:
InputStream stream = getClass().getResourceAsStream("/com/my/pkg/my_image.png");
See this SO question for some more examples:
how to add image from spectified package into label, frames etc
Edit: ClassLoader.getResourceAsStream(String) will find any resource that is located in the package you specify for the path. For example, if you want to get an image that resides in com.my.pkg.a from a class that resides in com.my.pkg.b:
// From com.my.pkg.b.MyClass
InputStream stream = getClass().getResourceAsStream("/com/my/pkg/a/my_image.png");
Notice that the path specified goes to package a. This will get find the image even though it resides in a different package. See the javadoc for ClassLoader.getResource(), which is what is used internally, for more detailed information.
create a folder in your root project folder ("resources" for instance)
in the project properties dialog, "Sources" page, click on the "Add
Folder..." button that is at the right of the "Source package
folders" table
Select your newly created folder, click OK
Change the label for your folder in the "Source package folders"
table ("Resource Packages" for instance)
The files in that folder will be included in the jar of your application.
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"));