aspose file tools*
The moose likes Java in General and the fly likes Properties questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Properties questions" Watch "Properties questions" New topic
Author

Properties questions

Pablo Fernandez C
Greenhorn

Joined: Aug 22, 2010
Posts: 2
Hi all! I am new to Java Ranch, I like this forum a lot. : )
I would like to ask about Properties.

I am programming a little application and although I have read many questions about this and documentation, I don't understand this topic very much and I don't always get it work. In my program, I have some .properties files with configuration data (as some file routes, seconds to wait in a loop inside the program...) so when I launch the program some values are going to be extracted from them. If I have to make some changes in these data, I can use the same jar modifying only the .properties files.

I am using Eclipse. And my code is organized in some packages:

src.main.java
src.main.java.util
src.main.java.view

The properties are going to be read from two different packages (default and util packages).

And I have my .properties files in a 'properties' folder (out of any package).

I began loading the Properties this way:



Later, I have implemented another method to load them in a different way but I don't know if it is better:



I have managed to get it work in Eclipse sometimes... but it seems that it doesn't work always... And if I export a JAR from Eclipse, I cannot manage to get it work either (it is worse indeed).


To clarify my doubts about this topic, I have some questions:

1) Which is the best way to load properties? I read somewhere some ways about the getResourceAsStream() and getResourceBundle(), but it seems easier to me the FileInputStream method. Is it worse?

2) Is it some way related the classpath to the Properties? (I don't think so but I am not sure...)

3) About question 2), the Properties should o should not be in the JAR? I think I got it work putting them in a near folder, but I was not very sure about what I was doing...

4) From where point are searched the Properties? Does the path begins in the default package so if the property is "/a.properties", should it be in the default package? If it is only "a.properties", where is it searched from?

5) In Eclipse, in the Project properties->Java Build Path, I can set my 'properties' folder as 'Class Folder' and it is added to Libraries. I also can add it as a source folder. I do not know if these options make a difference in the exportation. Do they?

6) The thing that get me more nervous is that it works sometimes, but I do not understand why it is not always or never. I have read here:
http://download.oracle.com/javase/tutorial/essential/environment/properties.html
that "the application creates another Properties object and loads the properties that were saved from the last time the application was run."

I think that this is the reason because it works sometimes for me...
Why does it work like this? When are 'these data from the last time' deleted?

Thank you very much for reading this long long paragraph, I have many doubts...

Regards!
Claude Moore
Ranch Hand

Joined: Jun 24, 2005
Posts: 453
    
    1

Hi Pablo and welcome !
Pablo Fernandez C wrote:
3) About question 2), the Properties should o should not be in the JAR? I think I got it work putting them in a near folder, but I was not very sure about what I was doing...


Well, as usual.. it depends. In this case, I'd say that it depends on which reason you're using props for.... anyway, most of times, one uses properties to store in an external
file some values by which make parametric own program. And, since props file are simply textual files, they're easily editable, too, so one can change on the fly values, if it's needed
for some reason.
So, If I were you I won't use properties files embedded in a Jar file. In my humble opinion, in most case it's useless....

Regards !
Claude.
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

Claude Moore wrote:
So, If I were you I won't use properties files embedded in a Jar file. In my humble opinion, in most case it's useless....

Regards !
Claude.


Just wanted to add something on that:

Properties files are changeable, so wherever/whoever is using the JAR may decide to change the properties to suit their needs / environment

If you update your code, to supply bug fixes etc, then give them a new JAR, they will deploy that and lose their old properties

Ideally they would have them backed up, but its possible they'd lose them, so like Claude says, best to keep them outside a JAR


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Too difficulat a question for "beginning". Moving thread.
Eduardo Yañez Parareda
Ranch Hand

Joined: Oct 09, 2008
Posts: 92

Pablo Fernandez C wrote:
To clarify my doubts about this topic, I have some questions:

1) Which is the best way to load properties? I read somewhere some ways about the getResourceAsStream() and getResourceBundle(), but it seems easier to me the FileInputStream method. Is it worse?


I always go for reading it with getResourceAsStream because the path is relative to the classpath, so if you want to put it out of the JAR, it's easy to do it.


3) About question 2), the Properties should o should not be in the JAR? I think I got it work putting them in a near folder, but I was not very sure about what I was doing...


It depends on what you want to do. If the configurations properties are something that people need to change, them it's better to have them out of the JAR.


4) From where point are searched the Properties? Does the path begins in the default package so if the property is "/a.properties", should it be in the default package? If it is only "a.properties", where is it searched from?


If you use getResourceAsStream, it's relative to the classpath, else is relative to the class that loads the file.

http://serfj.sourceforge.net - Simplest Ever REST Framework for Java
Pablo Fernandez C
Greenhorn

Joined: Aug 22, 2010
Posts: 2
Thank you very much for your responses.
Ok, so I will probably keep the properties outside the jar. What you described is my case, I want the properties easily be modified independently from the jar.
And do you know anything about what I read about the saved properties?
6) The thing that get me more nervous is that it works sometimes, but I do not understand why it is not always or never. I have read here:
http://download.oracle.com/javase/tutorial/essential/environment/properties.html
that "the application creates another Properties object and loads the properties that were saved from the last time the application was run."


This thing gets me very intrigued.

Thank you again!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Properties questions