• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Including property file in the jar

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.





 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Will it work for unix machines also.






java.lang.ClassLoader.getResourceAsStream()
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try here first...
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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");"
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread is becoming too complicated for beginners. Moving.
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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

Any Advantages ?

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic