How can I read a properties file from a .jar?

When I make a .jar file, In yhe manifest file, I do something like this:

Manifest-Version: 1.0

Main-Class: jeta.adapter.tcp.JetaTCPWrapper

Class-Path: comm.jar commons-codec-1.3.jar

I read a propertes file: jeta.properties

if this file is inside the .jar, no problem, it works correctly. But I need jeta.properties out fo the .jar. I want the the file in the same sirectory than the .jar.

緾an I do it writing something in the manifest file?

[486 byte] By [jorgiria] at [2007-11-27 5:30:45]
# 1
Properties props = new Properties();props.load(MyClass.class.getResourceAsStream("/jeta.properties"));Something like that.
bsampieria at 2007-7-12 14:55:18 > top of Java-index,Java Essentials,Java Programming...
# 2

It takes a bit of work to get "out of the jar". Put this code in a method

that you call and example the url displayed.

import java.security.*;

import java.net.*;

...

Class cls = someObject.getClass();

ProtectionDomain domain = cls.getProtectionDomain();

CodeSource source = domain.getCodeSource();

URL url = source.getLocation();

JOptionPane.showMessageDialog(null, url.toString());

That should give you a "file:" protocol URL for the jar containing the .class file involved.

I'm confident that you can parse this URL and use it to construct a path to the external resource you need.

Hippolytea at 2007-7-12 14:55:18 > top of Java-index,Java Essentials,Java Programming...
# 3

> It takes a bit of work to get "out of the jar". Put

> this code in a method

> that you call and example the url displayed.

> > import java.security.*;

> import java.net.*;

>

> ...

>

> Class cls = someObject.getClass();

> ProtectionDomain domain = cls.getProtectionDomain();

> CodeSource source = domain.getCodeSource();

> URL url = source.getLocation();

> JOptionPane.showMessageDialog(null, url.toString());

>

> That should give you a "file:" protocol URL for the

> jar containing the .class file involved.

> I'm confident that you can parse this URL and use it

> to construct a path to the external resource you need.

Although allowing your classloader to do all the heavy lifting, as in the previous post, is a lot simpler :-)

georgemca at 2007-7-12 14:55:18 > top of Java-index,Java Essentials,Java Programming...
# 4

> Although allowing your classloader to do all the heavy lifting, as in the previous post, is a lot simpler :-)

Willing to bet a donut? The thing is, the resource is *outside the jar*, and methods like getResource(AsStream) don't look outside the jar, even

when given hopeful relative paths like "../../oh/please/oh/please/file.properties" ;-)

Hippolytea at 2007-7-12 14:55:18 > top of Java-index,Java Essentials,Java Programming...
# 5
Good point... misread the OP.
bsampieria at 2007-7-12 14:55:18 > top of Java-index,Java Essentials,Java Programming...
# 6

> > Although allowing your classloader to do all the

> heavy lifting, as in the previous post, is a lot

> simpler :-)

>

> Willing to bet a donut? The thing is, the resource is

> *outside the jar*, and methods like

> getResource(AsStream) don't look outside the jar,

> even

> when given hopeful relative paths like

> "../../oh/please/oh/please/file.properties" ;-)

throws BlindlyFollowingBSampieriInNotReadingTheWordOutsideException() {}

:-)

I'll bet you a donut, but refer you straight to Dr Lazlo, who already owes me one :-)

georgemca at 2007-7-12 14:55:18 > top of Java-index,Java Essentials,Java Programming...
# 7

Ok, I磍l try it. I磎 sure that it磍l work.

Other way to do it is making a variable path: example: PLUGIN_PATH when I call the jar file:

java -DPLUGIN_PATH=/home/invitado/Documentos/plugin_PE/data -jar ./workspace/Jeta407/jeta407TCP.jar 5000

And I use the PLUGIN_PATH in load resource.

regards.

jorgiria at 2007-7-12 14:55:18 > top of Java-index,Java Essentials,Java Programming...
# 8

> I'll bet you a donut, but refer you straight to Dr Lazlo, who already owes me one :-)

Then we're even. I am/was Dr Jamf! I got tired of posters missing the

reference to Gravity's Rainbow and thinking I was a professor or sumthin'.

Also, cracking 10K posts and racking up a platinum star gave the

id too much gravitas. I'm just a modest code monkey at heart, so

I decided to go by another name, and shed my skin again. I had a

user name before DrJ, too, but that's ancient history.

Hippolytea at 2007-7-12 14:55:18 > top of Java-index,Java Essentials,Java Programming...
# 9
All true, now I bid adieu...
DrLaszloJamfa at 2007-7-12 14:55:18 > top of Java-index,Java Essentials,Java Programming...
# 10

> > I'll bet you a donut, but refer you straight to Dr

> Lazlo, who already owes me one :-)

>

> Then we're even. I am/was Dr Jamf! I got tired of

> posters missing the

> reference to Gravity's Rainbow and thinking I was a

> professor or sumthin'.

>

> Also, cracking 10K posts and racking up a platinum

> star gave the

> id too much gravitas. I'm just a modest code monkey

> at heart, so

> I decided to go by another name, and shed my skin

> again. I had a

> user name before DrJ, too, but that's ancient history.

I thought something was fishy when donuts were first mentioned. I knew a combined butcher/baker was a bad idea. Still, nice candlesticks

georgemca at 2007-7-12 14:55:18 > top of Java-index,Java Essentials,Java Programming...
# 11

> Then we're even. I am/was Dr Jamf! I got tired of

> posters missing the

> reference to Gravity's Rainbow and thinking I was a

> professor or sumthin'.

/me feels proud for actually having found the reference, and simultaneously somewhat ashamed for having been bored enough to google the name...

> Also, cracking 10K posts and racking up a platinum star gave the

> id too much gravitas. I'm just a modest code monkey at heart, so

> I decided to go by another name, and shed my skin again. I had a

> user name before DrJ, too, but that's ancient history.

I feel... so... deceived.

kevjavaa at 2007-7-12 14:55:18 > top of Java-index,Java Essentials,Java Programming...
# 12
> I feel... so... deceived.A mango lassi will cool you down.
ParvatiDevia at 2007-7-12 14:55:18 > top of Java-index,Java Essentials,Java Programming...