Strange resource loading problem

J

JavaEnquirer

I've had an Eclipse project for quite some time which successfully
loads images from an images folder. The class which loads the image
resides in an external jar, but the images themselves live in the main
project jar. Everything has been working fine, both when I deploy the
application in jars and when I run it in Eclipse.

However, I've recently made a change whereby the images only load when
I deploy the jar, not when I run from within Eclipse. That's all I have
done is move my top level source package from being directly under the
Project, to a sub folder called src/dev. The code still compiles
perfectly and runs up until the image load. This I can't understand
because the output folder location hasn't changed at all, so the
relative path between the class files and my images folder within the
project is exactly the same. I wouldn't have thought that moving the
source code should effect the "executables" and getting resources as
stream, but it does! Aarrghh! Any ideas anyone??

OLD:

MyProject
- images ( images folder )
- output ( class files )
- uk ( source package hierarchy )
- blah

NEW:

MyProject
- images ( images folder )
- output ( class files )
- src
- dev
- uk ( source package hierarchy )
- blah
 
O

opalpa opalpa

Before getting resource as stream execute get resource and display the
URL of the resource to make sure it is getting loaded from where you
think it is getting loaded.

opalpa
(e-mail address removed)
http://opalpa.info/
 
J

JavaEnquirer

When executed via the jar, I get a sensible URL path to the images.
When run from Eclipse it is null. This makes no sense as the relative
path from the codebase to the images hasn't changed since moving the
source code. It doesn't feel like a coding error as I've used my image
loading utility classes, numerous jars, Ant build files etc in loads
and loads of projects with no problems. This feels like an Eclipse
problem? Simply moving the folder in which the source is stored has
messed things up, but only when launching from Eclipse.
 
J

JavaEnquirer

Some more info if anyone's interested. When I execute my application
from within Eclipse the location of the class files as determined by
themselves is the same regardless of where the source is stored as I
expected. Why should moving the source affect get resource as stream?

Class objClass = MyClass.class;
ProtectionDomain pdomain = objClass.getProtectionDomain() ;
CodeSource codeSource = pdomain.getCodeSource() ;
URL codeLocation = codeSource.getLocation() ;

System.out.println("WHERE AM I: "+codeLocation);
 
J

JavaEnquirer

PROBLEM SOLVED, though not quite understanding how:

Project - Properties - Order and Export

Ranking the my four source folders from lowest level to highest seems
to have worked e.g.

MyProject/src/dev
MyProject/src/test
MyProject/src
MyProject
 
O

opalpa opalpa

getResource and getResourceAsStream have rules for looking. These
rules are more encompasing then the classes' directories. For example
say you add getResource() somewhere in your code but neglect to add
copy command to your build. You might test your code and the resource
will load but it will get picked up from source tree which you didn't
expect.

Can you revert to previous location of source tree and see what URL you
get from getResource()?

I reckon the key questions are: what is eclipse running exactly and
what are the differences in what it is running from when you run app
independelty? And is there a difference in the classloader (which I
believe defines the rules for where getResource searches)?



opalpa
(e-mail address removed)
http://opalpa.info/
 
O

opalpa opalpa

I prefer NetBeans to Eclipse. NetBeans has never messed up a project.
Eclipse has, in the five times or so I've used it, always messed up
whatever project I imported.

opalpa
(e-mail address removed)
http://opalpa.info/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top