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:
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...
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....
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.
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.