coffeemonitor
coffeemonitor

Reputation: 13120

Eclipse - referencing Java resources

This is probably just the IDE specifics. But with Eclipse, I'm interested in organizing my classes and resources a little more-so.

As I understand it, the defaulted "src" directory is the place to throw all my resources. And if my resources reside in there I can reference them like this:

ImageIcon icon = new ImageIcon("src/icon-16x16.png");

If I create a directory just for icons (inside the src/ directory), then I simply change the path as such:

ImageIcon icon = new ImageIcon("src/icons/icon-16x16.png");

So, now I want to do a separate directory for my resources outside the src/ directory.

Here's my old structure:

prj.HelloWorld  (project)
-> src/ .........
-> src/images/ ........
-> src/ (default package) ......classes.java

Here's what I've cleaned up:

prj.HelloWorld  (project)
-> classes/ .........
-> images/ ........
-> media / ........

Now, I'm interested in using a dedicated Project (prj.Resources), just for my common resources I can share in all projects.

prj.Resources  (project)
-> global_classes/ .........
-> global_images/ ........
-> global_media / ........

prj.HelloWorld  (project)
-> classes/ .........
-> images/ ........
-> media / ........

And then modify prj.HelloWorld by adding prj.Resources to the Projects Tab inside the Java Build Path.

(prj.HelloWorld > Properties > Java Build Path > Projects Tab > Add...)

I've done that so far, and any classes inside the prj.Resources/global_classes/ are successfully being detected.

But images inside global_images are not.

ImageIcon icon0 = new ImageIcon("global_images/icon.png");

I know we can use resources located in different projects, but I'm missing something obvious here.

Upvotes: 3

Views: 5304

Answers (3)

nitind
nitind

Reputation: 20003

Learn about how to construct your ImageIcon from a URL and how to find a URL to it relative to the location of the compiled .class file. Anything not a .java file is copied to the output folder automatically by Eclipse, so your image will also go there. Doing it this way allows your program to keep working whether you package it in a .jar file or not, and whether they're in different projects or not, as long as the Java Build Path states their relationships correctly (it's used to determine the runtime classpath, which is searched by Class#getResource()).

Upvotes: 0

Smit
Smit

Reputation: 4715

YOu can not access global_images folder as its not in its root folder for HelloWorld Project.

But you can give relative path as showing that project resource folder.

      ImageIcon icon0 = new ImageIcon("../Resources/global_images/icon.png");

Path Information

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109547

You could use the maven build infrastructure. There is a good eclipse integration.

Maven is in heavy use. Maven uses best praxises / conventions like directory trees.

It does the library dependency management and provides many build tools.

  • src/main/java - java sources
  • src/main/resources - for instance image resources
  • src/main/webapp - Web application root directory
  • src/test/java - junit tests
  • src/test/resources - test resources
  • target - build directory

There are features like multimodule projects, parent projects, inheriting parent's dependency version management.

Upvotes: 1

Related Questions