Reputation: 43
I searched before asking and none of those techniques work. I tried to put images under package, and this failed too. So how can I successfully export the jar file from Eclipse which includes images?
Upvotes: 3
Views: 450
Reputation: 6832
You need to put your images inside of a source folder and ensure that the settings for that folder don't exclude what you are trying to export.
Try setting up a clean Java project to ensure that it all works as expected with your version of eclipse to see what may be missing. Create a new project following these instructions using the project dialog:
File
> New
> Java Project
Project layout
> Configure default...
change the source folder name to src/main/java
which is the convention followed by tools like gradle and maven so it is the project layout for most opensource projects so its a good convention to stick with. Create new source folder
give it the name src/main/resources
which is again the standard naming. Create the new project and drag-drop an image into src/main/resources
e.g. test.jpg
then add a java class:
public class Test {
public static void main(String[] args) throws Exception {
System.out.println(Test.class.getClassLoader().getResourceAsStream("test.jpg").available());
}
}
Run it in eclipse to check it gives you the image file size. Then do File
> export
> JAR file
and export it to /tmp/test.jar
and run it with java -cp /tmp/test.jar
to see that it gives you the output.
That all works for me. Go back through that project settings and compare it with the one you are trying to setup. Note that there are options to include/exclude files which get exported by filename so it may be the case what you have an export filter which is stopping your files being exported. My recommendation is that you put all code under src/main/java
and all resources such as images and config files under src/main/resources
then have no export filters which is how build tools such as maven and gradle do things.
Upvotes: 0
Reputation: 10082
MAVEN is my suggestion
If you're following the standard Maven project directory structure then it is best to put all non-Java resources under src/main/resources. For example, you could create a subdirectory images, so that the full path would be src/main/resources/images. This directory would contain all your application images.
A special care should be taken to properly access images when application is packaged. For example, the following function should do everything you need.
public static Image getImage(final String pathAndFileName) {
final URL url = Thread.currentThread().getContextClassLoader().getResource(pathAndFileName);
return Toolkit.getDefaultToolkit().getImage(url);
}
This function can be used as getImage("images/some-image.png") in order to load some-image.png file in the image directory.
If ImageIcon is required then simply calling new ImageIcon(getImage("images/some-image.png")) would do the trick.
Upvotes: 1
Reputation: 343
In Eclipse: You can place image files along with Java source code files, if you are putting it in other directory then make sure you create that directory as src directory. And create packages same as source code packages, and copy image files to those created packages.
For example:
src (source directory)
^-------------------com.myproject
^................ Main.java
resource (source directory)
^-------------------com.myproject
^................ logo.png
Then try to export this project as jar file.
Upvotes: 0