I'm trying to make a jar, but the program needs images. When I ran the jar, the images didn't show up. However, they did in eclipse. I used this code:
label.setIcon(new ImageIcon("res/img/icon.png"));
Then I went looking on the internet for a way to fix it. I found this question here on StackOverflow, but when i tried it in my code, it's throwing a NullPointerException (also in eclipse). This is my code now:
label.setIcon(new ImageIcon(getClass().getResource("/res/img/icon.png")));
The images are in a separate folder in the package, called "res":
create a package inside your project, and name it something like "Images".
Now, add images you are using into this package.
Finally, call to these images, which are inside the package.
make sure you typed image names correctly as well
That is the best way of dealing with images
update
try
label.setIcon(new ImageIcon(getClass().getResource("res/img/icon.png")));
instead of
label.setIcon(new ImageIcon(getClass().getResource("/res/img/icon.png")));
Have a look that your RES folder is actually within the eclipse SRC folder.
Sometimes, users add a folder to the eclipse project instead.
Also:
Try cleaning and rebuilding your project.
In Eclipse, open the Navigator view. Open the /bin folder of the project. Does it have the /res/img/icon.png ( comment from here )
Related
I wrote code that works whenever i run in Eclipse, but whenever i try to export it into a runnable Jar, my images dont show up.
For my images this is the code being used pic1=ImageIO.read(FourCournersRunner.class.getResource("/Images/1.png"));
When exporting the project I've chosen "Package required libraries into generated JAR":
Please also show the code where you display the image in GUI, the problem might lie in there.
Did you also package the Images folder to the .jar file?
The problem might be caused by the way you set up build path too.
In short, I don't think the code you provide right now can reproduce the problem.
CONTEXT FOR MY PROBLEM
I am creating a Project using java swings on netbeans IDE 7.1.2 plateform on
windows m/c.Then I'm executing the jar file created on windows m/c in a linux m/c(cent os).
In a part of my project i needed to display some pictures in run time.
PROBLEM STATEMENT
Case 1)
Now what happened is that in run time i brought those pictures from some other location to src directory of my project by executing cp(copy) command through java code.
And yes i have checked those pictures were copied inside src directory successfully.
After that when i tried to display those images in a label,pictures were not being displayed.
So i wondered why is that happening.
Case 2)
After this problem i made a change and i imported those pictures inside src folder of my project before "cleaning and building" the project on windows m/c instead of bringing them in run time on linux m/c.Then after this when i executed the jar file on linux m/c,pictures were getting displayed in label.
So only change i made was that i included the pictures in src directory during "cleaning and building" my project on windows m/c.
So what i don't understand is that
why pictures are not getting displayed in 1st case.
MY QUESTIONS AND DOUBT
1) In context to resources like files and pictures(which are being used in project and i'm not talking about source codes of project here),what exactly happens when a jar file is created ? Are those resources(files & pictures) also included in jar while building the project or not?
2) Why i'm facing such a problem ?(And i have also checked the permissions on those image files.They were exactly same in both cases.)
Any insights that any of you can provide will be deeply appreciated.
So I assume you ar doing some thing like
label.setIcon(new ImageIcon("src/path/to/image"));
or
BufferdImage img = ImageIO.read(new File("src/path/to/image"));
or worse, using absolute file references...
Netbeans will add the content of of your src directory to the resulting jar file when the project is built (excluding your source files ;)), this changes the way in which these resources need to be treated, you can no longer treat them as you would if they were files on the file system.
Once embedded within your application context (such as been included in your jar file), they become what is commonly known as embedded resources.
Instead, you need to use Class#getResource or Class#getResourceAsStream depending on your needs, for example...
label.setIcon(new ImageIcon(getClass().getResource("path/to/image")));
Updated with after more details
Image im=newImageIcon(this.getClass().getResource("/home/aman/Desktop/diya.jpg")).getImage();
Is the wrong approach for his type of file. This file is a file on the file system, instead you should be using something more like...
BufferedImage image = ImageIO.read(new File("/home/aman/Desktop/diya.jpg"));
jLabel1.setIcon(new ImageIcon(image));
Note, ImageIO.read throws an IOException, which will be expected to catch, this is useful for diagnosing missing files or bad image formats
Unless you have previously changed the visibility state of the label, there is no need to call setVisible, in fact, you're setting label invisible anyway...
Swing application and it's Images are running fine in eclipse. but when i run from jar file, images not displaying. I tryed to load images in different way, but some time jar file not executing.
My image folder is inside of source folder.
I load my images with:
1.Running fine in eclipse, but not in jar
ImageIcon right = new ImageIcon("./chack img/Green Tick 1.PNG");
2.Running fine in eclipse, but jar file not executing.
ImageIcon wrong = new javax.swing.ImageIcon(getClass().getResource("/chack img/Red Cross1.PNG"));
I tryed some other code also, still my issue not solved.
Put your images in the src folder, along with your classes. Optional, but make a package that stores your images. Compiling a jar file will not keep any directories that are not a source directory.
Also, remove all spaces from your folders and file names. This prevents confusion, and is likely part of your problem. No spacing is a good practice to follow in programming, and in most situations of naming folders and files.
Also, kiheru is correct, you should not have capital letters in the file extensions, for the same reason you should not have spaces; It can cause confusion and it is a bad practice in most cases.
EDIT: Simmilar Question
I've been working on a little project that requires external images for display. I'm not all that familiar with how to use Eclipse and this is my first time attempting to export a completed project so I can share it with others. Right now, it seems the only way I can get my images to show up is if I assign a specific folder on my hard drive and have the image paths in the code go to that.
I'm looking for a way to export the images as part of my JAR or as part of the same package so when I go to send this program to other people, I don't have to send them a separate archived folder of images. I'd also be interested in learning what I need to do to have my code reference the images within that package so they'll work without an external folder.
I have read about some kind of package system within Eclipse, but have thus far had no luck in figuring out how to use it. Could use some explicating!
Thanks in advance to anyone willing to give me their two cents.
Something I would have found useful with this answer is the following: make sure you put your images/files in the same eclipse folder (or sub-folder below) as your source code. I created a folder "images_ignored" using eclipse, added it to the build path but still it refused to be included in my JAR file (when creating an executable JAR).
Just drag the images folder into your Eclipse project, then choose to "Copy New Folder" or "Copy File and Folder" depending on Eclipse version, and then right click on the image folder (in Eclipse) and --> build path "use as source folder".
you might need to load them as class path resources if they are within a jar. see: getClass().getClassLoader().getResourceAsStream(...)
Use getResource() to load the images:
ImageIcon qmarkIcon = new ImageIcon(getClass().getResource("images/mark.gif"));
If you're using JDK 1.7 or JDK 1.8, you might want to use the NIO.2 API.
for (FileSystemProvider provider : FileSystemProvider.installedProviders()) {
if ("jar".equals(provider.getScheme()))
return provider.newFileSystem((new File(Start.class
.getProtectionDomain().getCodeSource().getLocation().toURI()))
.toPath(), new HashMap<String, Object>());
}
If you enter this code into a method that returns a java.nio.file.FileSystem, you can call the method to get the FileSystem for the JAR file.
To get the path to access the files inside your JAR file, you can use the following method, which then allows you to read the files however you may want.
fileSystem.getPath("/images/image.gif")
If you would like to be able to run this in Eclipse, make sure you surround the call to the method with a try/catch IOException and assign to your FileSystem object the following.
new File(Start.class.getProtectionDomain().getCodeSource().getLocation().toURI())
.toPath().toString();
This will allow you to run your program whether it's compressed into a JAR file or not.
I recommend you get used to using NIO.2, since it is a very powerful API.
If you add a folder to build path you can retrieve the images either in eclipse and when you exported it in jar file, just remember to don't reference the image with the path like img/myImage.gif but only myImage.gif !
I'm doing a simple Java project on Netbeans. And I'm stuck not knowing how to add my own image into the code. It kept throwing IOException no matter where I put the image. This sounds ridiculous but it really got me stuck.
If you run the program from netbeans, the root folder should be the same as the root of the project folder. That is, the folder which contains build.xml and manifest.mf.
If putting your image directly in that folder, and loading it without any path given doesn't work, then you will have too look at your code and see if you can find any errors.
Try looking at you project directory and find where the html/xhtml files are. Mostly it will be located inside the web directory within the project directory projectname/build/web. That will be source for the image tag in your html page you can directly give the name of the image for "src".