In the process of Java development, sometimes it is necessary to choose to execute different scripts or load different dynamic libraries according to the type of operating system. For example, the script under Windows is .bat
file, and the script under Linux .sh
file, and dynamic library under Windows is .dll
file, and under Linux is .so
file.
If you want to know the type of the current operating system, you can judge it by the system attribute os.name
, and the system attribute is obtained by the System.getProperty(os.name)
method. Let's analyze it first System
Relevant source code in the class.
1, System class source code analysis
System
The class is located in the java.lang
package, it is a final
class, which means that this class cannot be inherited, and its class definition is as follows:
public final class System {
/** Don't let anyone instantiate this class */
private System() {
}
}
The constructor is private and cannot be instantiated.
The method to get system properties is getProperty()
, which has two overloaded methods, as follows:
-
public static String getProperty(String key)
: The parameter key is the attribute name; -
public static String getProperty(String key, String def)
: The parameter key is the property name, and the parameter def represents the default value. When the specified property key has no value, the default value def is used instead.
Let's take a look at the source code of these two methods:
(1) getProperty(String key)
public static String getProperty(String key) {
// 检查属性名
checkKey(key);
// 获取安全管理器
SecurityManager sm = getSecurityManager();
if (sm != null) {
// 检查属性访问权限
sm.checkPropertyAccess(key);
}
// 获取并返回属性值
return props.getProperty(key);
}
Obtaining system properties usually goes through three steps:
- Check if property name is empty
- Check property access
- get attribute value
Among them checkKey()
method source code is as follows:
private static void checkKey(String key) {
if (key == null) {
throw new NullPointerException("key can't be null");
}
if (key.equals("")) {
throw new IllegalArgumentException("key can't be empty");
}
}
This method mainly judges whether the attribute name is null or an empty string, if it is null, it will throw NullPointerException
exception, if it is an empty string, it will throw IllegalArgumentException
exception .
SecurityManager
is a security manager, a class used to implement security policies, check whether the application has permissions for certain operations, and eventually call AccessControlContext
class of checkPermission()
method, if the application does not have permission to operate, it will throw AccessControlException
exception.
The final attribute value is obtained by the Properties
getProperty()
method of the Properties
class. Defined as follows:
private static Properties props;
private static native Properties initProperties(Properties props);
And the initProperties()
method is called in the initializeSystemClass()
method. The source code is as follows:
private static void initializeSystemClass() {
props = new Properties();
initProperties(props); // initialized by the VM
//....
}
About the Properties
class getProperty()
method is defined as follows:
public String getProperty(String key) {
Object oval = super.get(key);
String sval = (oval instanceof String) ? (String)oval : null;
return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
}
(2) getProperty(String key, String def)
public static String getProperty(String key, String def) {
checkKey(key);
SecurityManager sm = getSecurityManager();
if (sm != null) {
sm.checkPropertyAccess(key);
}
return props.getProperty(key, def);
}
The difference between this method and the previous method is that it can set a default value. When the attribute value does not exist, the default value will be used instead, and the getProperty(String key, String defaultValue)
method of ---cbb32a84fe4529974f8dafe4dba27df9 Properties
is called. It is defined as follows:
public String getProperty(String key, String defaultValue) {
// 首先获取属性值
String val = getProperty(key);
// 如果属性值为 null,则返回默认值
return (val == null) ? defaultValue : val;
}
2. Code implementation
package com.magic.system;
public class SystemUtils {
/**
* 判断操作系统是否是 Windows
*
* @return true:操作系统是 Windows
* false:其它操作系统
*/
public static boolean isWindows() {
String osName = getOsName();
return osName != null && osName.startsWith("Windows");
}
/**
* 判断操作系统是否是 MacOS
*
* @return true:操作系统是 MacOS
* false:其它操作系统
*/
public static boolean isMacOs() {
String osName = getOsName();
return osName != null && osName.startsWith("Mac");
}
/**
* 判断操作系统是否是 Linux
*
* @return true:操作系统是 Linux
* false:其它操作系统
*/
public static boolean isLinux() {
String osName = getOsName();
return (osName != null && osName.startsWith("Linux")) || (!isWindows() && !isMacOs());
}
/**
* 获取操作系统名称
* @return os.name 属性值
*/
public static String getOsName() {
return System.getProperty("os.name");
}
}
3. Test verification
The test verification code is as follows:
public static void main(String[] args) {
System.out.println("os.name : " + getOsName());
System.out.println("isWindows : " + isWindows());
System.out.println("isLinux : " + isLinux());
System.out.println("isMacOs : " + isMacOs());
}
Running under Windows 10 system environment, the output is as follows:
os.name : Windows 10
isWindows : true
isLinux : false
isMacOs : false
Running in the CentOS 7 system environment, the output is as follows:
os.name : Linux
isWindows : false
isLinux : true
isMacOs : false
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。