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 / February 2007

Tip: Looking for answers? Try searching our database.

Access Data Inside JAR

Thread view: 
Jason Cavett - 26 Feb 2007 22:35 GMT
If this is not the place to ask, I'd appreciate it if you could direct
me to the correct place.

I am creating an executable JAR from a project that has the following
structure (I'm developing in Eclipse):

Project
- src files
- resources
   - icons
   - config

Inside the config directory is a default config file.  If the user
does not have a personalized config file, I want the default one to be
read into an object and then, when the user is finished, that object
will be written to their home directory so they have a personalized
config file in the future.

The problem is, I can't seem to access the config file.  I can access
the icons in the icons folder, but I'm stuck on how to get to the
config file.  (I tried ClassLoader.getSystemResource("resources/config/
config.xml") but that always throws a NullPointerException.)

If anybody could point me in the right direction, I'd appreciate it.

Thanks
Andrew Thompson - 26 Feb 2007 23:08 GMT
..
> The problem is, I can't seem to access the config file.  I can access
> the icons in the icons folder, but I'm stuck on how to get to the
> config file.  (I tried ClassLoader.getSystemResource("resources/config/
> config.xml") but that always throws a NullPointerException.)

Really?  AFAIU, getSystemResource() might return
a null URL, but it does not actually throw NPE's
itself.

OTOH, I would usually *not* use the system class
loader for my own application's resources.
Instead, I recommend you try plain old
'getResource()'.

Once done, print the URL.  If it is 'null',
it might be that this call has been done too
early in the process, using the 'root'
(or whatever Sun calls it) class loader that
is really only good for classes.

Sometimes this can happen if you call it in
the program's main(), for example.  It is
usually better to attempt it from within
(for example) a constructor for the class.

HTH

Andrew T.
Philipp - 27 Feb 2007 08:24 GMT
> If this is not the place to ask, I'd appreciate it if you could direct
> me to the correct place.
[quoted text clipped - 20 lines]
>
> If anybody could point me in the right direction, I'd appreciate it.

Probably you want to read the data out of this config file, so you
should use getResourceAsStream(String name) which will return an
InputStream.

HTH
Phil
Philipp - 27 Feb 2007 09:21 GMT
>> If this is not the place to ask, I'd appreciate it if you could direct
>> me to the correct place.
[quoted text clipped - 24 lines]
> should use getResourceAsStream(String name) which will return an
> InputStream.

OTOH you might be interested in the fact that there is a Preferences
class (java.util.prefs.Preferences) which can handle the config stuff of
your app in a nice and platform independent way (also providing defaults
when a value is not found).

Phil
Jason Cavett - 27 Feb 2007 14:59 GMT
> >> If this is not the place to ask, I'd appreciate it if you could direct
> >> me to the correct place.
[quoted text clipped - 33 lines]
>
> - Show quoted text -

Thanks for your help Phillip and Andrew.  But, the resource is still
coming back as "null" no matter what I try to do.  Here's the portion
of my code in question...

URL mainURL = this.getClass().getResource("resources/config/
config.xml");
(I also tried - URL mainURL = this.getClass().getResource("/resources/
config/config.xml"); )

This always returns null.  However, this works (if the file isn't
packaged in a JAR):

File mainFile = new File("resources/config/config.xml");

Any other suggestions?  All this config file is is a default file that
is used if the user does not have their own personalized config file,
so I don't need to write to this config file - only read it.
Philipp - 27 Feb 2007 15:25 GMT
>>>> If this is not the place to ask, I'd appreciate it if you could direct
>>>> me to the correct place.
[quoted text clipped - 44 lines]
> is used if the user does not have their own personalized config file,
> so I don't need to write to this config file - only read it.

That's why I told you to use getResourceAsStream(). AFAIK you can't open
the URL you get back from getResource() as a File (don't ask me why).

If you actually want to read something from it use getResourceAsStream()

Maybe you also want to use:
getClass().getClassLoader().getResourceAsStream("resources/config/config.xml");

or in a static method:
Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/config/config.xml")

Phil
Jason Cavett - 27 Feb 2007 16:31 GMT
> >>>> If this is not the place to ask, I'd appreciate it if you could direct
> >>>> me to the correct place.
[quoted text clipped - 59 lines]
>
> - Show quoted text -

The problem isn't in reading what I'm getting back...it's the fact
that I'm not getting anything back at all...

InputStream stream = getClass().getClassLoader().getResourceAsStrea("/
resources/config/config.xm­l");
System.out.println(stream);

Output...

null

It's extremely annoying because I can get the images (which are in
resources/images) just fine.
Jason Cavett - 27 Feb 2007 16:41 GMT
> > >>>> If this is not the place to ask, I'd appreciate it if you could direct
> > >>>> me to the correct place.
[quoted text clipped - 75 lines]
>
> - Show quoted text -

Well...now I'm confused...

When I run the code as an executable JAR, it works the way you said it
should (using an InputStream from the getResourceAsStream(String)).
But, when I run the code from Eclipse, the direct file method works.
Each method does not work with the other way of running the
application (and results in a FileNotFoundException or
NullPointException).

I prefer the JAR method, but, I still want to be able to run my code
via Eclipse since it's great for debugging, etc.

Any suggestions on this?
Chris Uppal - 27 Feb 2007 17:46 GMT
> URL mainURL = this.getClass().getResource("resources/config/
> config.xml");

That will be looking for the config.xml in a place relative the class's
location in the jar.  E.g. if "this" is an instance of my.stuff.MyClass, then
the above will be looking for:
   my/stuff/resource/config/config.xml

If that isn't what you want then you should adjust accordingly.  It may help to
know that when you ask the class's classloader for the resource (instead of
asking the class itself directly) the path you provide is taken relative to the
root of the containing JAR file (or directory tree).

   -- chris
Jason Cavett - 28 Feb 2007 03:05 GMT
On Feb 27, 12:46 pm, "Chris Uppal" <chris.up...@metagnostic.REMOVE-
THIS.org> wrote:
> > URL mainURL = this.getClass().getResource("resources/config/
> > config.xml");
[quoted text clipped - 10 lines]
>
>     -- chris

Okay, well, that makes sense (and works when I try it).  But why
doesn't this work within Eclipse?  If I use the System classloader, it
works fine in the executable JAR but does not work when I run it
within Eclipse?  (I know this is more of an Eclipse question - but it
is annoying that I can't do the same thing in one method of running
that I can do in another method.)

Thanks for your help.
Philipp - 28 Feb 2007 07:36 GMT
> On Feb 27, 12:46 pm, "Chris Uppal" <chris.up...@metagnostic.REMOVE-
> THIS.org> wrote:
[quoted text clipped - 18 lines]
> is annoying that I can't do the same thing in one method of running
> that I can do in another method.)

It should. It works here, both ways (eclipse & jar).
Phil


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.