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.
Related
I have setup a project in eclipse, which has the default src folder and a newly created source folder called rsc. Inside the rsc folder, there is an audio file called sound.wav, which I am accessing with the following code:
InputStream input = this.getClass().getClassLoader().getResourceAsStream("sound.wav");
And this works just fine, when I put it into a Clip for example I can play the sound. However, if I move the file into the src folder, the file is no longer found and the method returns null. I did try different permutations of the url like the following, but none worked.
"/sound.wav"
"src/sound.wav"
"/src/sound.wav"
When looking at the project properties under the source tab, both folders are marked as source folders and have the same settings. So the question remains, why can java only find the resource inside the rsc folder, but not inside the src folder?
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'm trying to get an image in this way:
Node rootIcon = new ImageView(new Image(this.getClass().getResourceAsStream("folder.png")));
I guess that I need to place the image in another folder or to set the path to it in other way. The image is in src\main\resources\icon and the class itself is in src\main\java\package\package\package\class
how to find out what path I need to set or where shall I put the image to?
Try .getResourceAsStream("/icon/folder.png"). If you use maven then, the content of src/main/resouces is automatically added to your jar file. Eclipse for example adds that folder to the calls path, if it is a source folder. So running from Eclipse should work too.
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 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"));