This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like an eclipse-related question, so I shall move it to our IDEs forum.
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, thanks. Sorry for posting in the wrong forum.

-Russ
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out, and thank you for providing the answer.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic