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.
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 ).
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.
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.
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.
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)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With