Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 879
coffeemonitor Avatar asked Jun 11 '26 04:06

coffeemonitor


1 Answers

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

like image 120
Smit Avatar answered Jun 13 '26 08:06

Smit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!