首页 > 系统相关 >Java代码判断当前操作系统是Windows或Linux或MacOS

Java代码判断当前操作系统是Windows或Linux或MacOS

时间:2023-02-28 18:13:24浏览次数:37  
标签:MacOS getProperty String Windows static key Java null public

在Java开发过程中,有时候需要根据操作系统的类型,来选择执行不同的脚本或加载不同的动态库,比如 Window下的脚本是 .bat 文件,而 Linux 下的脚本是 .sh 文件,还有 Windows 下的动态库是 .dll 文件,而 Linux 下是 .so 文件。

如果想要知道当前操作系统的类型,可以通过系统属性 os.name 来判断,而系统属性具体是通过 System.getProperty(os.name) 方法获取的,下面先来分析一下 System 类中的相关源码。

1、System类源码分析
System 类位于 java.lang 包下,它是一个 final 类,也就表示该类是不能被继承的,它的类定义如下:

public final class System {

/** Don't let anyone instantiate this class */
private System() {
}
}

其中构造函数是私有的,不能被实例化。

获取系统属性的方法是 getProperty(),该方法有两个重载方法,如下:

public static String getProperty(String key):其中参数 key 就是属性名;
public static String getProperty(String key, String def):参数 key 就是属性名,而参数 def 表示默认值,当指定属性 key 没有值时,使用默认值 def 替代。
下面分别看看这两个方法的源码:

(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);
}

获取系统属性,通常会经历三个步骤:

检查属性名是否为空
检查属性访问权限
获取属性值
其中 checkKey() 方法源码如下:

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");
}
}

该方法主要判断属性名是否为 null 或者为空字符串,如果为 null ,则会抛出 NullPointerException 异常,如果为空字符串,则会抛出 IllegalArgumentException 异常。

SecurityManager 是安全管理器,用于实现安全策略的类,检查应用程序是否具有某些操作的权限,最终会调用 AccessControlContext 类的 checkPermission() 方法,如果应用程序没有操作权限,则会抛出 AccessControlException 异常。

最终属性值的获取是通过 Properties 类的 getProperty() 方法获取的,所有的系统属性都会保存在 Properties 实例中,由JVM进行初始化,其定义如下:

private static Properties props;
private static native Properties initProperties(Properties props);

而 initProperties() 方法又是在 initializeSystemClass() 方法中调用的,源码如下:

private static void initializeSystemClass() {
props = new Properties();
initProperties(props); // initialized by the VM
//....
}

关于 Properties 类的 getProperty() 方法定义如下:

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);
}

该方法和上一个方法的区别在于它可以设置一个默认值,当属性值不存在时,则会使用默认值替代,调用的是 Properties 的 getProperty(String key, String defaultValue) 方法,其定义如下:

public String getProperty(String key, String defaultValue) {
// 首先获取属性值
String val = getProperty(key);
// 如果属性值为 null,则返回默认值
return (val == null) ? defaultValue : val;
}

2、代码实现
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、测试验证
测试验证代码如下:

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());
}

在 Windows 10 系统环境下运行,输出结果如下:

os.name : Windows 10
isWindows : true
isLinux : false
isMacOs : false

在 CentOS 7 系统环境下运行,输出结果如下:

os.name : Linux
isWindows : false
isLinux : true
isMacOs : false
————————————————
版权声明:本文为CSDN博主「然笑后端」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ranxiaotop/article/details/125993529

标签:MacOS,getProperty,String,Windows,static,key,Java,null,public
From: https://www.cnblogs.com/telwanggs/p/17165476.html

相关文章

  • java网络编程-待测试
    publicclassServer2{publicstaticvoidmain(String[]args)throwsException{System.out.println("==服务器的启动==");//1.注册端口......
  • git push 时出现 "error:unpack failed:error Java heap space"
    原因:推送的远程服务器的文件太大了解决方法:方法一:git--no-thinpush方法二:使用gitlfs可以在网上找方法如何使用:        windows下先下载:lfs软件 ......
  • JavaScript Window Screen
    JavaScript WindowScreenwindow.screen对象包含有关用户屏幕的信息。WindowScreenwindow.screen对象在编写时可以不使用window这个前缀。一些属性:screen.ava......
  • JavaScript Window - 浏览器对象模型
    JavaScript Window-浏览器对象模型浏览器对象模型(BOM)使JavaScript有能力与浏览器"对话"。浏览器对象模型(BOM)浏览器对象模型(Browser Object Model(BOM)......
  • JavaScript Window Location
    JavaScript WindowLocationwindow.location对象用于获得当前页面的地址(URL),并把浏览器重定向到新的页面。WindowLocationwindow.location 对象在编写时可不使......
  • 抓包工具之Charles(windows)
    PC端如何配置才能抓取到https请求:1.安装证书:在顶部工具栏中选择“help--InstallCharlesCASSLCertificate”;   2.然后会弹出证书信息,选择安装证书,接下来将证书存储改......
  • 解决Java读取数据库的时间类型时大8小时时差问题
    解决办法:在jdbc连接中设置serverTimezone参数,指定为东八区,可以使用serverTimezone=Asia/Shanghai或者serverTimezone=GMT%2b8如:jdbc:mysql://127.0.0.1:3306/demo_ds......
  • JavaScript 闭包
    JavaScript 闭包JavaScript变量可以是局部变量或全局变量。私有变量可以用到闭包。全局变量函数可以访问由函数内部定义的变量,如:实例functionmyFunction(){......
  • JavaScript Array(数组) 对象
    JavaScript Array(数组) 对象数组对象的作用是:使用单独的变量名来存储一系列的值。在线实例创建数组,为其赋值:实例varmycars=newArray();mycars[0]="Saab......
  • JavaScript Math(算数) 对象
    JavaScript Math(算数) 对象Math(算数)对象的作用是:执行常见的算数任务。在线实例round()如何使用round()。random()如何使用random()来返回0到1之间的随机数......