Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between System.getProperty and properties.getProperty in Java

Tags:

java

I have a properties file, named prop.properties. In my main code, I have both System.getProperty() and properties.getProperty().

My question is: are they both get property from prop.properties or they will get property from different places, properties.getProperty() get property from prop.properties and System.getProperty() get property from other place.

like image 342
Jay Zhang Avatar asked Jun 17 '13 15:06

Jay Zhang


People also ask

What is the difference between system Getenv and system getProperty?

getenv() is for Operating System environment variables, whereas System. getProperty() is for JVM arguments which are passed as -DpropName=value to Java application launcher ( java ).

What does system getProperty do in Java?

The getProperty(String key) method in Java is used to returns the system property denoted by the specified key passed as its argument.It is a method of the java.

What is system properties in Java?

Java™ system properties determine the environment in which a Java program runs by starting a Java virtual machine with a set of values. You can choose to use the default values for Java system properties or you can specify values for them by adding parameters to the command line when you start your application.

What is the difference between setProperty and getProperty?

The setProperty and getProperty action tags are used for developing web applications with Java Beans. In web development, The Bean class is mostly used because it is a reusable software component that represents data. The jsp:setProperty action tag sets a property value or values in a Bean using the setter method.


Video Answer


3 Answers

System.getProperty() gets a property as defined by the JVM (either the JVM itself or any -D options you may have passed at the command line). The list of defined properties can be found here (thanks @NikitaBeloglazov).

properties.getProperty() is the result of someone having initialized an object of type Properties. They are not the same, though you can get what System has as a Properties instance.

A Properties object is very often the result of loading a Java property file (see here for how this is done)

like image 191
fge Avatar answered Oct 23 '22 21:10

fge


System.getProperty(propName) is a shortcut for System.getProperties().getProperty(propName).

However java.util.Properties is just a subclass of java.utils.Hashtable, so its instance may be created everywhere in code and populated with any data. Obviously code

Properties props = System.getProperties();
props.getProperty("os.name");

is the same as

System.getProperty("os.name");

However

Properties props = new Properties();
props.load(new FileInputStream("myprops.properties"))
props.getProperty("os.name");

is not the same.

like image 36
AlexR Avatar answered Oct 23 '22 23:10

AlexR


The System class refers to the JVM you are running (which would get info from your OS). When you use getProperty on System you get actual properties.

The Property class is basically a glorified hash table. You can completely define it yourself, so when you do getProperty() you get the results that you set up. The usefulness of the Property class is that it has a built in XML parser so you can read in properties from a file.

like image 4
David says Reinstate Monica Avatar answered Oct 23 '22 21:10

David says Reinstate Monica