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.
Related
i have been having a problem for a while where when i export my eclipse project into an executable jar file it wont run correctly. I think the resources folder is the source of this problem but i'm not sure, because all that happens whenever i run it is the JFrame pops up. for some reason i cant take screenshots because my computer is a piece of crap, but I've shared what i know. thank you, all help is much appreciated!
UPDATE:
this has been tagged as duplicate, but i have tried their methods and it wouldn't work. i need instructions specific for my situation.This is the link to the picture of my file explorer took a little while back when i had the same problem, because i cant take the picture now it is not up to date.
I am having trouble loading Images contained within a JAR file. I have read a number of other posts related to this. But cannot find the answer. Now here is where it gets complicated.. This all works fine if I am using a Runnable JAR file exported from Eclipse and Run it using the standard JRE. However This JAR file is actually a plugin for a piece of software called pro/ENGINEER which has it's own JRE that is used to run the JAR file. The strange thing is.. that this works fine on the development machine, that has eclipse installed and so on, but doesn't work on any of the client machines?! But I can't see what is different.
I am trying to load the image using:
ImageIcon icon = new ImageIcon(this.getClass().getResource("resources/Header.png");
This method is called from my 'Start' class, and the Jar is structured as follows:
load
Start.java
load.resources
Header.png
If I Open the Jar file with WinRar you can see the Image definitely exists in the jar, in this position.
I know this may be a very specific question but if anyone can be of any help that would be great.
I did not have the particular problem you're having now, however I ran into the problem of not finding a resource in a JAR file several times in the past.
The solution was to not get the resource from the class, but from the class loader as demonstrated below:
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource("resources/Header.png"));
Mind the .getClassLoader() call after the getClass().
Hope this helps.
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 )
I've seen this topic in this forum but it I need a more basic explanation on how to do this.
I've done a program in Java with some external libraries (LWJGL and Slick).
So this is what I've done and my program won't start anyway, tell me where I've done wrong.
I have Eclipse 3.7.1
My project is opened in Eclipse and runs well in Eclipse
I click File -> Export
I select Java -> Runnable JAR file
Here I don't know what to choose in Launch configuration, when I click the dropdown I get the option to choose my main class so I do that.
I select an export destination
I select the option "Package required libraries into generated JAR" under Library Handling
I don't know what ANT script is so I don't use that
I click Finish
I copy my images-folder to the same location as the generate JAR-file
I try to start the JAR-file, something loads in the background but nothing happens, no window shows up, nothing.
I check the Task manager in windows and sees that a javaw.exe is running
What did I miss?
My program uses images for graphics like this:
image = new Image("images/filname.png");
I wonder if I need to change the paths before exporting or the method to load these?
Thanks!
If you want to get things from inside a jar file you need to have them in your classpath and access them as resources.
The constructor you use, refers to a physical file which cannot peek inside a jar-file.
(EDIT) Also note that you have no guarantee where the current working directory is. Hence any relative references may break, as you see.
Read this to learn how to use JarSplice to export your Eclipse project to a runnable jar-file. Regarding images, you can put them in the same directory as a class file (in the jar) and then writeSampleClass.class.getResourceAsStream("image.png")to retrieve an InputStream of the image. Then you can load it however you like.
I had the same problem and I was able to fix it. All I did was copy the data folder (which contains my resources) into the *.jar file. You can do this for example with WinRAR.
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 !