Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Strange resource loading problem

Thread view: 
JavaEnquirer - 12 Oct 2006 11:51 GMT
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
opalpa opalpa@gmail.com http://opalpa.info - 12 Oct 2006 13:53 GMT
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
opalpa@gmail.com
http://opalpa.info/
JavaEnquirer - 12 Oct 2006 14:55 GMT
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.

> 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
[quoted text clipped - 3 lines]
> opalpa@gmail.com
> http://opalpa.info/
JavaEnquirer - 12 Oct 2006 15:20 GMT
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);
JavaEnquirer - 12 Oct 2006 15:34 GMT
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
opalpa opalpa@gmail.com http://opalpa.info - 12 Oct 2006 15:44 GMT
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
opalpa@gmail.com
http://opalpa.info/
JavaEnquirer - 12 Oct 2006 15:52 GMT
Cheers opalpa, thank you for your input.

> 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
[quoted text clipped - 3 lines]
> opalpa@gmail.com
> http://opalpa.info/
opalpa opalpa@gmail.com http://opalpa.info - 12 Oct 2006 15:41 GMT
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
opalpa@gmail.com
http://opalpa.info/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.