String workDirectory = System.getProperty("user.dir");
Many students must have written or seen the above code. So have you ever thought about what does "user.dir" mean? Is it a system environment variable? Or user folder?
As the saying goes, "Ask the source code for everything", let us go to the source code to find out:
public static String getProperty(String key) {
checkKey(key);
SecurityManager sm = getSecurityManager();
if (sm != null) {
sm.checkPropertyAccess(key);
}
return props.getProperty(key);
}
// 本地方法,用于初始化props属性
private static native Properties initProperties(Properties props);
From the source code above, we can see that the getProperty method obtains the corresponding value from a static property props of the System class, and the props property is given an initial value through the local method initProperties. That is, this system property is automatically initialized when the JVM starts by executing the native method. Fortunately, jdk's documentation notes have explained to us which properties the JVM ensures, and we list them in the following table:
attribute name | illustrate | example value |
---|---|---|
java.version | Java version number | 11.0.5 |
java.version.date | Java version date | 2019-10-15 |
java.vendor | Java vendor specific string | Oracle Corporation |
java.vendor.url | Java provider URL | http://java.oracle.com/ |
java.vendor.version | Java vendor version | 18.9 |
java.home | Java installation root directory | /usr/lib/jvm/jdk-11.0.5 |
java.class.version | Java class file version number | 55.0 |
java.class.path | Java classpath | Too long, omitted here |
os.name | operating system name | Linux |
os.arch | operating system architecture | amd64 |
os.version | operating system version | 5.0.0-37-generic |
file.separator | file separator | / |
path.separator | path separator | : |
line.separator | line break | \n |
user.name | user account | lhing17 |
user.home | user root directory | /home/lhing17 |
user.dir | user's current working directory | /home/lhing17/IdeaProjects/rocketDownloader |
The example values in the above table are the results output by the test class on my personal computer. We can clearly see that "user.dir" refers to the user's current working directory. If you are running the project in the IDE, this directory is the root directory of your current project. In addition to "user.dir", some other useful properties are provided here, such as "java.home", "user.home" and so on. The JVM (referring to the java virtual machine) guarantees that the properties in the above table are valuable. In the project, we can safely obtain them through System.getProperty(propertyName) without worrying about null pointer exceptions.
We can also modify these properties by means of System.setProperty(propertyName, propertyValue), or customize some new properties. However, the official java documentation clearly reminds us that modifying these system properties may cause unexpected side effects, and we should not usually modify these properties.
Summarize
The properties obtained by the System.getProperty method are properties initialized when the JVM is loaded, such as "user.dir" representing the user's current working directory. The JVM ensures that some specified property values exist, and we can safely call them.