Runnable Jars Missing Images/Files (Resources)

Runnable JARs missing Images/Files (Resources)

Seems like you not putting your stuff in the right sense. In order to make it work, follow these steps :

  1. Right-Click your Project in Project Explorer Tree.
  2. Go to New -> Source Folder and then provide any Name to the Source Folder.
  3. Now manually add your stuff to this Source Folder so created by you, like if you want to add images then make a New Folder, by manually visiting this Source Folder through File System.
  4. Name this New Folder as images and copy your images to this Folder.
  5. Now go back to your Eclipse IDE and Refresh your Project from the Project Explorer, by Right Clicking your Project, here you be able to see your added content now after refreshing.

Now in order to access, say any image, you will use.

getClass().getResource("/images/yourImageName.extension");

which will return one URL object. Do remember the first forward slash, in this case, since whatever is inside your Source Folder is accessed with the help of this, in simpler terms. Now when you will Run your project, the content of this Source Folder will be automatically added to the bin folder and when you will create a Runnable Jar, then the stuff inside your Source Folder can be accessed as it is.

Eclipse exported Runnable JAR not showing images

The problem was I had this project in my Windows profile... that had an "!" in it... (DeNitE! -> was the name of my Windows profile)

As soon as I changed it to DeNitE (without the !) it worked fine...

ImageIcon lost after creating a runnable JAR file

I am using this method to read image into BufferedImage where IconManager is class where it is defined.

private static BufferedImage readBufferedImage (String imagePath) {
try {
InputStream is = IconManager.class.getClassLoader().getResourceAsStream(imagePath);
BufferedImage bimage = ImageIO.read(is);
is.close();
return bimage;
} catch (Exception e) {
return null;
}
}

Runnable Jar cannot find Resources and Other Libraries

For the toolbar image you need to add a slash, i.e. instead of

this.getClass().getResourceAsStream("images/search_folder.png")

you need

this.getClass().getResourceAsStream("/images/search_folder.png")

This is because, as explained in the JavaDocs, Class.getResourceAsStream resolves relative paths against the package of the class in question, so if this is a com.example.Foo then getResourceAsStream("images/search_folder.png") would look for com/example/images/search_folder.png inside your JAR. Prepending the slash would make it look for images/search_folder.png instead, which is what your screenshot suggests you need.

You will need to use a similar trick for the GoogleMap.html - you can't load items from inside a JAR using java.io.File, but you could use this.getClass().getResource("/GoogleMap.html") to get a java.net.URL pointing to the HTML file inside your JAR.

Runnable Jar File doesn't load resources

you should add the image in your classpath or a directory in your classpath
and then load your image like this:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource("src/de/therealjan/tools/pictures/pic.png");
ImageIcon icon = new ImageIcon(resource);

Text and image files not included when exporting jar

If you are both reading and writing to a file, then locating this file in the application jar is not appropriate as mentioned in the other answer: you should persist your data at an external location.

However, it is usual to keep the read-only resources files (such as images) in the jar. If you want to keep this approach for the images and possibly other resources, you are facing two problems:

  1. Getting Eclipse to include the file in the jar using the Export Runnable Jar feature.

  2. Finding the file in the jar

Including the file

The simplest is probably just to place the file in a source folder. In your project, do New -> Source Folder, give it a name (e.g., "resources"), and move your file there. Normally, if you re-run the export, the file should be in the jar.

Finding the file

Files in jar are accessed differently. See the accepted answer to Reading a resource file from within jar. Note that you don't need to include the name of your resource folder in the path, as this file will be placed at the root of your jar (you can verify this by unpacking it).



Related Topics



Leave a reply



Submit