File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes IDEs, Version Control and other tools and the fly likes Where Do I Place My Resource Files So Eclipse Will Include Them in the Jar? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Where Do I Place My Resource Files So Eclipse Will Include Them in the Jar?" Watch "Where Do I Place My Resource Files So Eclipse Will Include Them in the Jar?" New topic
Author

Where Do I Place My Resource Files So Eclipse Will Include Them in the Jar?

Russ Russell
Ranch Hand

Joined: Jan 24, 2010
Posts: 72
Hi,
I am creating a runnable/executable jar for a program that uses some image files on disk. When I hard-code the explicit location of the files, the jar executes just fine. But since the end users won't have the image files (and even if they did, they would not be located in the same place as my code points to), I want to make a relative reference to the location, and include them in the jar.

Here is the code I am using:


No matter where I place the image files, they do not end up in the jar, and therefore the jar crashes with the following exception:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at com.russ.Board.<init>(Board.java:28)
at com.russ.GameApp.main(GameApp.java:8)

There is no place in Eclipse to "Add Files" to the jar.

My class files are located at: C:\JavaCode\Game\bin\com\russ
I tried putting the image files in each of those sub-directories and they never end up in the jar.

Why aren't the image files being included in the jar? What am I doing wrong? Any help is appreciated.

Thanks in advance,
-Russ S.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Sounds like an eclipse-related question, so I shall move it to our IDEs forum.
Russ Russell
Ranch Hand

Joined: Jan 24, 2010
Posts: 72
Okay, thanks. Sorry for posting in the wrong forum.

-Russ
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

What kind of project did you create in Eclipse? What is the project directory structure? Have you tried placing the PNG file in the source directory?


JBoss In Action
Russ Russell
Ranch Hand

Joined: Jan 24, 2010
Posts: 72
Resolved.

Okay, here's the answer.
Eclipse needs the resource files to reside in two places. One place so that the IDE can use them, and in another place so that they can be included in the jar. Placing them in just one of these locations will not take care of the other problem. They need to reside in both places.

The two places are:
1) The root of the bin directory (NOT inside of a sub-directory there)
2) In a sub-directory under the project folder.

Eclipse has an easy way to add the sub-directory for location #2. Right click the project and select New > Source Folder.

As long as the files live in both places, the IDE can use them AND they will be included in the jar. Thank you to Ralph Cook and Wouter Oet for helping me narrow down this problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Well done sorting it out, and thank you for providing the answer.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Hm.... would you mind answering Peter's questions? It is highly dependent on what type of application and how you build it. Copying the resources in two places should not be needed.

Have you done a "Project clean" on your project? Probably it cleans your bin folder and does not copy the resource again - which is a hint.

If bin is your output folder copying a resource there means it is available in the default package at runtime.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Where Do I Place My Resource Files So Eclipse Will Include Them in the Jar?