Java System.getProperty() Guide
In Java, the System.getProperty() method can be used to retrieve system properties. This method takes a key value for a system property as a parameter and returns its value. Here is a simple example:
public class Main {
public static void main(String[] args) {
// 获取Java版本
String javaVersion = System.getProperty("java.version");
System.out.println("Java 版本:" + javaVersion);
// 获取操作系统名称
String osName = System.getProperty("os.name");
System.out.println("操作系统名称:" + osName);
// 获取用户的工作目录
String userDir = System.getProperty("user.dir");
System.out.println("用户工作目录:" + userDir);
}
}
The example above demonstrates how to use the System.getProperty() method to retrieve the Java version, operating system name, and user’s working directory. To run this example, you can save the code above as Main.java file, compile it using javac Main.java in the command line, and run it using java Main.