Jars and getResource()  
Author Message
Markos Charatzas





PostPosted: 2004-7-13 20:34:00 Top

java-programmer, Jars and getResource() Hi there,

I've been trying to find a solution to this with no luck so far.

Consider a project with the following dir structure

/bin
/resources
/resources/velocity
.
.
etc.

In /resources/velocity there is a file "velocity.properties"

with /resources/velocity in the classpath I can simply say from within a
class

Class.class.getResourceAsStream(/velocity.properties);

and get an InputStream to that file.


However, when packaging it in a jar file, the above statement stops
working even if in the Manifest I specify in the header:

Class-Path: resources/velocity/


Any ideas? :(
TIA
 
Stefan Waldmann





PostPosted: 2004-7-13 21:09:00 Top

java-programmer >> Jars and getResource() Markos Charatzas wrote:
> Hi there,
>
> I've been trying to find a solution to this with no luck so far.
>
> Consider a project with the following dir structure
>
> /bin
> /resources
> /resources/velocity
> .
> .
> etc.
>
> In /resources/velocity there is a file "velocity.properties"
>
> with /resources/velocity in the classpath I can simply say from within a
> class
>
> Class.class.getResourceAsStream(/velocity.properties);
>
> and get an InputStream to that file.
>
>
> However, when packaging it in a jar file, the above statement stops
> working even if in the Manifest I specify in the header:
>
> Class-Path: resources/velocity/
>
>
> Any ideas? :(
> TIA

what about

Class-Path: /resources/velocity

int the manifest file, or instead:

Class.class.getResourceAsStream("/resources/velocity/velocity.properties");

(not tested, but the latter should work even without Class-Path
definition in the manifest file)

Regards,
Stefan
 
Markos Charatzas





PostPosted: 2004-7-13 21:11:00 Top

java-programmer >> Jars and getResource() Thanx for the prompt response.

when specifying the class path,
You need the "/" at the end when specifying a directory.
I thought that that "/" in the beginning defines the root of the filesystem.

/resources/velocity wont work anyway because of the missing "/" at the
end anyway.

the full path in the getResource() will work for sure :) but does that
mean that everytime I have to have different ways to access that
resource depending on whether is a packed into a jar or not? :|


Stefan Waldmann wrote:
>
> Markos Charatzas wrote:
>
>> Hi there,
>>
>> I've been trying to find a solution to this with no luck so far.
>>
>> Consider a project with the following dir structure
>>
>> /bin
>> /resources
>> /resources/velocity
>> .
>> .
>> etc.
>>
>> In /resources/velocity there is a file "velocity.properties"
>>
>> with /resources/velocity in the classpath I can simply say from within
>> a class
>>
>> Class.class.getResourceAsStream(/velocity.properties);
>>
>> and get an InputStream to that file.
>>
>>
>> However, when packaging it in a jar file, the above statement stops
>> working even if in the Manifest I specify in the header:
>>
>> Class-Path: resources/velocity/
>>
>>
>> Any ideas? :(
>> TIA
>
>
> what about
>
> Class-Path: /resources/velocity
>
> int the manifest file, or instead:
>
> Class.class.getResourceAsStream("/resources/velocity/velocity.properties");
>
> (not tested, but the latter should work even without Class-Path
> definition in the manifest file)
>
> Regards,
> Stefan
 
 
Andrew Thompson





PostPosted: 2004-7-13 21:45:00 Top

java-programmer >> Jars and getResource() On Tue, 13 Jul 2004 16:11:26 +0300, Markos Charatzas wrote:

> I thought that that "/" in the beginning defines the root of the filesystem.

Why think, try it.

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
 
Stefan Waldmann





PostPosted: 2004-7-13 21:55:00 Top

java-programmer >> Jars and getResource() Markos Charatzas wrote:
> Thanx for the prompt response.
>
> when specifying the class path,
> You need the "/" at the end when specifying a directory.
> I thought that that "/" in the beginning defines the root of the
> filesystem.
>
> /resources/velocity wont work anyway because of the missing "/" at the
> end anyway.

sorry, didn't think of that. Does it work with
/resources/velocity/
??

> the full path in the getResource() will work for sure :) but does that
> mean that everytime I have to have different ways to access that
> resource depending on whether is a packed into a jar or not? :|

No, it doesn't. If the directory which holds "resources/velocity" is in
your classpath, the application should find your resource even when you
run it "unjared".

HTH
Stefan
 
 
Markos Charatzas





PostPosted: 2004-7-13 21:56:00 Top

java-programmer >> Jars and getResource()
I did try every possible combination of "/resources/velocity/" and didnt
work :(

I've also googled it and still I can't find a definite answer to it!

When packed in jar, do I have to use the relative path *inside* that jar
to access a resource? Why the Class-Path "doesnt work"? :/

Do I have to code both ways in case the app turns to a jar?


Stefan Waldmann wrote:
>
> Markos Charatzas wrote:
>
>> Thanx for the prompt response.
>>
>> when specifying the class path,
>> You need the "/" at the end when specifying a directory.
>> I thought that that "/" in the beginning defines the root of the
>> filesystem.
>>
>> /resources/velocity wont work anyway because of the missing "/" at the
>> end anyway.
>
>
> sorry, didn't think of that. Does it work with
> /resources/velocity/
> ??
>
>> the full path in the getResource() will work for sure :) but does that
>> mean that everytime I have to have different ways to access that
>> resource depending on whether is a packed into a jar or not? :|
>
>
> No, it doesn't. If the directory which holds "resources/velocity" is in
> your classpath, the application should find your resource even when you
> run it "unjared".
>
> HTH
> Stefan
 
 
Stefan Waldmann





PostPosted: 2004-7-13 22:06:00 Top

java-programmer >> Jars and getResource() Markos Charatzas wrote:
> I thought that that "/" in the beginning defines the root of the
> filesystem.

in "getResourceAsStream(...)" it means either the root of (inside) the
jar file, or the "root" of each directory that is specified in the
classpath (for instance if you have "/home/myprog:." as classpath, "/"
means "/home/myprog" as wall as ".")

Regards,
Stefan
 
 
Markos Charatzas





PostPosted: 2004-7-13 22:06:00 Top

java-programmer >> Jars and getResource() Ok I see... But still I dont see where the solution to the problem might
be. :(

However, thanks for all of your input so far

Stefan Waldmann wrote:
>
> Markos Charatzas wrote:
>
>> I thought that that "/" in the beginning defines the root of the
>> filesystem.
>
>
> in "getResourceAsStream(...)" it means either the root of (inside) the
> jar file, or the "root" of each directory that is specified in the
> classpath (for instance if you have "/home/myprog:." as classpath, "/"
> means "/home/myprog" as wall as ".")
>
> Regards,
> Stefan
 
 
Stefan Waldmann





PostPosted: 2004-7-13 22:09:00 Top

java-programmer >> Jars and getResource() Markos Charatzas wrote:
>
> I did try every possible combination of "/resources/velocity/" and didnt
> work :(
>
> I've also googled it and still I can't find a definite answer to it!
>
> When packed in jar, do I have to use the relative path *inside* that jar
> to access a resource? Why the Class-Path "doesnt work"? :/
>
> Do I have to code both ways in case the app turns to a jar?

No, like I stated in my other answer, just put the directory which holds
"/resources" into your classpath
e.g. you have
/home/myuser/myprog/resources/velocity

then put in your classpath

/home/myuser/myprog

Then your app should be able to find the resource, jar or not jar.
 
 
Andrew Thompson





PostPosted: 2004-7-13 22:09:00 Top

java-programmer >> Jars and getResource() On Tue, 13 Jul 2004 16:55:42 +0300, Markos Charatzas wrote:

> I did try every possible combination of "/resources/velocity/" and didnt
> work :(

It sounds like it is time for an SSCCE..
<http://www.physci.org/codes/sscce.jsp>

If we could see *exactly* what you are doing from
start to finish, your misunderstanding would
probably become clear.

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
 
Markos Charatzas





PostPosted: 2004-7-13 23:46:00 Top

java-programmer >> Jars and getResource() ARGGGGGGGGGGGGGGGGGGGGG!!!!!!!!!!!!!!!!!!!!!!

I just found out what's wrong!!!!

try indexing the jar and the getResource() and getResourceAsStream()
goes bye bye!!!!!!!!!

........
Sun really needs to look into this!
That's with 1.4.2_04. Let me try _05 ...



Markos Charatzas wrote:
> I have attached a small example
> Unzip to see contents
>
>
> Andrew Thompson wrote:
>
>> On Tue, 13 Jul 2004 16:55:42 +0300, Markos Charatzas wrote:
>>
>>
>>> I did try every possible combination of "/resources/velocity/" and
>>> didnt work :(
>>
>>
>>
>> It sounds like it is time for an SSCCE..
>> <http://www.physci.org/codes/sscce.jsp>
>>
>> If we could see *exactly* what you are doing from
>> start to finish, your misunderstanding would
>> probably become clear.
>>
>
 
 
Andrew Thompson





PostPosted: 2004-7-14 0:27:00 Top

java-programmer >> Jars and getResource() On Tue, 13 Jul 2004 18:46:13 +0300, Markos Charatzas wrote:

> try indexing the jar and the getResource() and getResourceAsStream()
> goes bye bye!!!!!!!!!

Sure.. where's your code?
 
 
Markos Charatzas





PostPosted: 2004-7-14 1:11:00 Top

java-programmer >> Jars and getResource() In the attachment I made in my earlier post

Also, I wrote it in my blog so you can check it there as well
http://cue.modblog.com/core.mod?show=blogview&blog_id=224078

Andrew Thompson wrote:
> On Tue, 13 Jul 2004 18:46:13 +0300, Markos Charatzas wrote:
>
>
>>try indexing the jar and the getResource() and getResourceAsStream()
>>goes bye bye!!!!!!!!!
>
>
> Sure.. where's your code?
 
 
Roedy Green





PostPosted: 2004-7-14 2:33:00 Top

java-programmer >> Jars and getResource() On Tue, 13 Jul 2004 15:33:38 +0300, Markos Charatzas
<email***@***.com> wrote or quoted :

>Class.class.getResourceAsStream(/velocity.properties);

see http://mindprod.com/jgloss/image.html
for some sample uses of getResource and getResourceAsStream
normally you leave off the lead / and put the resource in the same
package in the jar as the classes that use it.

At any rate you left off the surrounding "".


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
 
Roedy Green





PostPosted: 2004-7-14 2:35:00 Top

java-programmer >> Jars and getResource() On Tue, 13 Jul 2004 18:46:13 +0300, Markos Charatzas
<email***@***.com> wrote or quoted :

>try indexing the jar and the getResource() and getResourceAsStream()
>goes bye bye!!!!!!!!!

what does the term "indexing" the jar mean?

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
 
Markos Charatzas





PostPosted: 2004-7-14 14:21:00 Top

java-programmer >> Jars and getResource()
jar -i [jar name]

-i generate index information for the specified jar file


Roedy Green wrote:
> On Tue, 13 Jul 2004 18:46:13 +0300, Markos Charatzas
> <email***@***.com> wrote or quoted :
>
>
>>try indexing the jar and the getResource() and getResourceAsStream()
>>goes bye bye!!!!!!!!!
>
>
> what does the term "indexing" the jar mean?
>