• 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

Properties questions

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 1357
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 231
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficulat a question for "beginning". Moving thread.
 
Ranch Hand
Posts: 93
IntelliJ IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Pablo Fernandez C
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic