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.
Related
I've been making a small java program to use and I am almost done with it. I tried exporting it as a runnable jar in both eclipse and IntelliJ but the jar is not running. I tried surrounding the launcher class in a try catch statement to see why and the exception.getMessage() simply says location required. I'm Using JavaFX for my GUI. Keep in mind that when I click run in both IDEs it runs perfectly.
I read a similar question here but it seemed that the person couldn't even run it in the IDE so please don't reference me back to it.
Note: I extracted the jar to see if my package containing my fxml file is in the jar and it is.
Thanks in advance
I had the same problem. Issue with mine was I was using relative path to get resources. For example
Parent addUser = FXMLLoader.load(getClass().getResource("../fxml/addUserGUI.fxml"));
I was using this, after changing it to
Parent addUser = FXMLLoader.load(getClass().getResource("/BikeShop/fxml/addUserGUI.fxml"));
It worked. BikeShop is my package name here, is the root directory of the project.
I am a novice programmer trying to create a jar file that people can download and demonstrates a...JRPG-ish upgrade system I have. As a little visual pop, I want the central button to display a little icon as an extra visual pop. While it works fine in Eclipse, when I export it to a JAR file the icon is no longer displaying (though the rest of the program works as intended.
I have managed to make Eclipse import the files I want into the JAR file, as seen below.
This is the code I currently have to display them in Eclipse
imgDin = new ImageIcon("icons\Mark_of_Din.png");
imgNayru = new ImageIcon("icons\Mark_of_Nayru.png");
imgFarore = new ImageIcon("icons\Mark_of_Farore.png");
How can I modify this code to display them inside the JAR file?
You should be using
new ImageIcon(getClass().getResource("/Mark_of_Din.png"));
That little package icon in the icons folder icon, means the files in the icons folder will be at the root of the classpath. getClass() gets the calling class, and getResource() locates a resource relative to the class. The / brings the search to the root.
You can extract the jar and you will see the icons folder is not there.
See Related Post
"While it works fine in Eclipse.."
That's because when you pass a String to the ImageIcon constructor, it is looking for a file on the file system, relative to the working directory. In Eclipse (and most IDEs), the default working directory is the project root, and that's where your icons folder is.
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...
I'm trying to make a mp3 player and I used javazoom libraries. I save mp3 path in library.txt file to reopen them. There is not any problem with openning library.txt which is in the jar file.
Normally with eclipse program It is working well but when i created a jar file the problems begins.
I can't use absolute paths to open any mp3 file with my jar file.
File file = new File("/Users/orcungumus/Music/iTunes/iTunes Media/Music/Bruno Mars/Unorthodox Jukebox/1-16 16 Locked Out of Heaven.mp3");
try {
player.open(file);
} catch (BasicPlayerException e1) {
JOptionPane.showMessageDialog(null, e1.toString(),
"Error", JOptionPane.INFORMATION_MESSAGE);
}
For making thinks easier to understand i used directly path of an file. It works for eclipse, but with jar file it can't open file.
This is the error which i take:
If it is important i use mac os.
Edit: I realized that this is not about absolute path by making mp3 path relative. Libraries give an error about audio format. So, the problem is still exist. what can be the differencies between runnable jar file version and eclipse run for an project.
well, that's hard to find out.
now your problem is unable to open a audio file, which I can't handle.
However you said that the programme run perfectly using eclipse, so the question became
the different between running in eclipse and normal jar file.
FIRST, eclipse not directly use java.exe nor javaw.exe for execution. Instead, eclipse use some custom defined library in order to redirect the console, handling exception, etc.
(That is nothing to your problem)
SECOND, eclipse will set the working directory for you automatically (to the project root directory), where may contain some necessary native library.
THIRD, eclipse also define some path for native location for library, I think you may use eclipse built-in export to create the jar file which however not include the native library.
the second and third difference may help you, because they are quite effective to jar file.
I have just browsed javazoom briefly, I don't know whether there is native library. If not,
I sorry that you have to find other solution.
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 !