• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, thanks. Sorry for posting in the wrong forum.

-Russ
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done sorting it out, and thank you for providing the answer.
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic