From: Martin Gregorie on
I have a configuration file intentionally buried in a jar file I'm
building (and a tool to amend it before you ask). This is all working
correctly but could possibly have been done better.

Yesterday I searched for documentation on doing this type of thing but
couldn't find anything in the standard Java 6 documentation.
Embarrassingly, I can't even find the description of how to set it up
that I used in the first place. I'm probably missing something obvious,
so pointers on where to find this information would be most welcome.

The reason I need the documentation: I can write code that uses an
InputStream to read the configuration file provided its in the same
package as the class that reads it, but would prefer to put the file in
the root of the jar file. Is it mandatory for the file to be in the same
package as its reader or is there a way of accessing it when its placed
elsewhere?


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
From: Nigel Wade on
On Fri, 12 Feb 2010 14:55:08 +0000, Martin Gregorie wrote:

> I have a configuration file intentionally buried in a jar file I'm
> building (and a tool to amend it before you ask). This is all working
> correctly but could possibly have been done better.
>
> Yesterday I searched for documentation on doing this type of thing but
> couldn't find anything in the standard Java 6 documentation.
> Embarrassingly, I can't even find the description of how to set it up
> that I used in the first place. I'm probably missing something obvious,
> so pointers on where to find this information would be most welcome.
>
> The reason I need the documentation: I can write code that uses an
> InputStream to read the configuration file provided its in the same
> package as the class that reads it, but would prefer to put the file in
> the root of the jar file. Is it mandatory for the file to be in the same
> package as its reader or is there a way of accessing it when its placed
> elsewhere?

Specify a class within the relevant jar as the "reference" for getClass,
then use an absolute path for the resource itself.

--
Nigel Wade
From: John B. Matthews on
In article <hl411c$6f$2(a)south.jnrs.ja.net>,
Nigel Wade <nmw(a)ion.le.ac.uk> wrote:

> On Fri, 12 Feb 2010 14:55:08 +0000, Martin Gregorie wrote:
>
> > I have a configuration file intentionally buried in a jar file I'm
> > building (and a tool to amend it before you ask). This is all
> > working correctly but could possibly have been done better.
> >
> > Yesterday I searched for documentation on doing this type of thing
> > but couldn't find anything in the standard Java 6 documentation.
> > Embarrassingly, I can't even find the description of how to set it
> > up that I used in the first place. I'm probably missing something
> > obvious, so pointers on where to find this information would be
> > most welcome.
> >
> > The reason I need the documentation: I can write code that uses an
> > InputStream to read the configuration file provided its in the same
> > package as the class that reads it, but would prefer to put the
> > file in the root of the jar file. Is it mandatory for the file to
> > be in the same package as its reader or is there a way of accessing
> > it when its placed elsewhere?
>
> Specify a class within the relevant jar as the "reference" for
> getClass, then use an absolute path for the resource itself.

Here's an example:

<http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/src/org/gc
s/robot/RCImage.java?revision=64&view=markup>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
From: Martin Gregorie on
On Fri, 12 Feb 2010 12:24:48 -0500, John B. Matthews wrote:

> In article <hl411c$6f$2(a)south.jnrs.ja.net>,
> Nigel Wade <nmw(a)ion.le.ac.uk> wrote:
>
>> On Fri, 12 Feb 2010 14:55:08 +0000, Martin Gregorie wrote:
>>
>> > I have a configuration file intentionally buried in a jar file I'm
>> > building (and a tool to amend it before you ask). This is all working
>> > correctly but could possibly have been done better.
>> >
>> > Yesterday I searched for documentation on doing this type of thing
>> > but couldn't find anything in the standard Java 6 documentation.
>> > Embarrassingly, I can't even find the description of how to set it up
>> > that I used in the first place. I'm probably missing something
>> > obvious, so pointers on where to find this information would be most
>> > welcome.
>> >
>> > The reason I need the documentation: I can write code that uses an
>> > InputStream to read the configuration file provided its in the same
>> > package as the class that reads it, but would prefer to put the file
>> > in the root of the jar file. Is it mandatory for the file to be in
>> > the same package as its reader or is there a way of accessing it when
>> > its placed elsewhere?
>>
>> Specify a class within the relevant jar as the "reference" for
>> getClass, then use an absolute path for the resource itself.
>
> Here's an example:
>
> <http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/src/org/
gc
> s/robot/RCImage.java?revision=64&view=markup>

So the 'absolute path' takes the class's package as its root. Gotcha.


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
From: Robert Kochem on
Martin Gregorie schrieb:

> I have a configuration file intentionally buried in a jar file I'm
> building (and a tool to amend it before you ask). This is all working
> correctly but could possibly have been done better.
>
> Yesterday I searched for documentation on doing this type of thing but
> couldn't find anything in the standard Java 6 documentation.

Looks like you are missing the right keywords. YOu are searching for the
method "getResourceAsStream(String name)" which is present in the
ClassLoader as well as in each Class:

http://mindprod.com/jgloss/getresourceasstream.html

Robert
 |  Next  |  Last
Pages: 1 2 3
Prev: 64-bit JNI
Next: Java - GUI - Date time input box