Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Including property file in the jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Including property file in the jar" Watch "Including property file in the jar" New topic
Author

Including property file in the jar

ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Hi All,

Can any body help me how to include a property file along with the class files in the jar.

So that in any other system i can directly run this without setting the path for referring the property file.


I created a

propertyfile : abc.properties

java file : readProp.java

readProp.java reads the abc.properties since i have set the path in the java file as


prop.load(new FileInputStream("C:/abc.properties"));

while i take this file to another windows machine it should say

prop.load(new FileInputStream("D:/path/abc.properties"));

while i take this file to unix it should say

prop.load(new FileInputStream("/usr/abc.properties"));



Every time i need to make this changes and compile it, accordingly when i take it to different machine.


Is there a way to resolve this, so that i dont change the path every time.







Thanks


Discussion - the powerfull way to excellence!
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
You could try to load the property file as a ResourceBundle. This will pick the property file if it is in the Classpath.
So in the jar the property file should be bundled at the same level where the class package structure starts.
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Gamini Sirisena wrote:You could try to load the property file as a ResourceBundle. This will pick the property file if it is in the Classpath.
So in the jar the property file should be bundled at the same level where the class package structure starts.



How do i do that any code sample.

Will it work for unix machines also.





Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

ram kumar wrote:
How do i do that any code sample.

Will it work for unix machines also.






java.lang.ClassLoader.getResourceAsStream()

OCUP UML fundamental and ITIL foundation
youtube channel
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Try here first...
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Gamini Sirisena wrote:Try here first...


Is that possible to include that line of code to read as resourceBundle.

Later i will start using it in all my logics

ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
ram kumar wrote:
Gamini Sirisena wrote:Try here first...


Is that possible to include that line of code to read as resourceBundle.

Later i will start using it in all my logics



Hey guys please give me a source code,

I am not able to point to the right directory to read a property file as a resourceBundle

am getting filenotfound exception.

say c:\tmp in windows and /tmp in unix machines

i have given the below code too.

am placing the test.properties in the same ResourceBundle package only still ot able to read.

my objective is, -- i will not say the Absolute path of the test.properties,

wherever test.properties is there in the machine, my program should get it from windows machines as well as unix machines

exclusive of the path and location it is present !



Thanks
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

ram kumar wrote:Later i will start using it in all my logics


Hey Ram, honestly if you want to hard-code the path to the properties file bundle with jar, why don't you just generate it on the fly. Exactly what is in this properties file? Enter these things as properties -D when running java can be another option.

If the really want to bundle the ClassLoader.getResource(...) should work as is. Although the path of the properties file inside the jar needs to be fixed. And I don't suggest you put that file in the default package. Suppose your package structure is:



Put the file in say toppackage then you use "ClassLoader.getResource("toppackage/abc.properties");"


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
K. Tsang wrote:
ram kumar wrote:Later i will start using it in all my logics


Hey Ram, honestly if you want to hard-code the path to the properties file bundle with jar, why don't you just generate it on the fly. Exactly what is in this properties file? Enter these things as properties -D when running java can be another option.

If the really want to bundle the ClassLoader.getResource(...) should work as is. Although the path of the properties file inside the jar needs to be fixed. And I don't suggest you put that file in the default package. Suppose your package structure is:



Put the file in say toppackage then you use "ClassLoader.getResource("toppackage/abc.properties");"



Thanks First !

That was a valid input, also.

some how i modified the code to behave this way ..

what do you think about this code.

i set the test.properties in the same package, where DemoGetResourceAsStream.java resides

i mean package ResourceBundle.mainpackage;

It worked perfectly,

I am doubting as will it work for unix machines also, without no change in the code.






AnyMore comments and chages are welcome


K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

Looks ok to me. As long as you get the "expected" output once the Properties is loaded then you should be fine.

Well since you are loading from within jar, but if you are generating on the fly - just to get your head think, what happens if the properties file has read/write access permission problems.
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
K. Tsang wrote:Looks ok to me. As long as you get the "expected" output once the Properties is loaded then you should be fine.

Well since you are loading from within jar, but if you are generating on the fly - just to get your head think, what happens if the properties file has read/write access permission problems.


what should be done to change the access rights.

let that be part of the discussion .

Also,

This would be my important question like as get property , do we have a setProperty method that would set a property and value.

if setProperty is not there, how can we achieve it.

Is that just using fileoutputstream...


ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Hey All,

I searched how to set that in the properties

I did that too.

But, there lies the problem, am hard coding the path ,

since, prop.store uses fileoutputstream...

Is there any way to avoid this hard coding...

new FileOutputStream("C:/senet/Eclipse-ServerVersion/workspace/WorkOuts/src/ResourceBundle/mainpackage/test.properties");





Complete code :


K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

Good that you are using Properties.load and Properties.store - doesn't it make it easier to save file? LOL.

Like I said earlier depends on what you are saving. Suppose I have a server that access a database file that allows client to access remotely. What do I need to store for the server and client?

Server: database file path and port number
Client: server IP address and port number

So first time I run the server, I prompt for the database file and port number the server is going to. Similarly the first time the client runs, I prompt for which server IP/port to connect to.

How to prompt again up to you - can be Swing GUI or from the command prompt using Scanner and such.



Oh about file permissions, I don't think you can set that in coding. If it is inside jar, then of course it's read only and can't write.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
This thread is becoming too complicated for beginners. Moving.
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146



Also, why dont you use fileoutputstream instead of file and printwriter

Any Advantages ?

 
wood burning stoves
 
subject: Including property file in the jar
 
Similar Threads
parsing data and storing in the xml
How Can I read a .properties file
Not able to insert image more than 4K in Database
read properties file from web.xml or through java
Config.ini