• java 获取系统变量、环境变量


    1.情景展示

    java如何获取操作系统的环境变量?

    2.具体分析

    环境变量:对于系统来说,指的是:操作系统中的系统变量。

    系统变量:对于java来说,指的是:java自身配置的一些变量。

    3.解决方案

    如何获取java系统变量?

    通过System.getProperty("key")获取;

    可通过System.setProperty("key", "value")新增或修改java变量。

    遍历java系统变量

    for (Iterator<Map.Entry<Object, Object>> it = System.getProperties().entrySet().iterator(); it.hasNext();) {
        Map.Entry<Object, Object> entry = it.next();
        System.out.println(entry.getKey() + ":" + entry.getValue());
    }
    

    java设置的系统变量有:

    查看代码
    java.runtime.name:Java(TM) SE Runtime Environment
    sun.boot.library.path:D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\bin
    java.vm.version:25.261-b12  //Java 虚拟机实现版本
    java.vm.vendor:Oracle Corporation   //Java 虚拟机实现供应商
    java.vendor.url:http://java.oracle.com/ //Java 供应商的 URL
    path.separator:;    //路径分隔符(在 UNIX 系统中是":")
    java.vm.name:Java HotSpot(TM) 64-Bit Server VM  //Java 虚拟机实现名称
    file.encoding.pkg:sun.io
    user.country:CN
    user.script:
    sun.java.launcher:SUN_STANDARD
    sun.os.patch.level:
    java.vm.specification.name:Java Virtual Machine Specification   //Java 虚拟机规范名称
    user.dir:D:\workspace-idea\upload   //用户的当前工作目录
    intellij.debug.agent:true
    java.runtime.version:1.8.0_261-b12
    java.awt.graphicsenv:sun.awt.Win32GraphicsEnvironment
    java.endorsed.dirs:D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\endorsed
    os.arch:amd64   //操作系统的架构
    java.io.tmpdir:C:\Users\Marydon\AppData\Local\Temp\ //默认的临时文件路径
    line.separator:
        //行分隔符(在 UNIX 系统中是"/n")
    java.vm.specification.vendor:Oracle Corporation //Java 虚拟机规范供应商
    user.variant:
    os.name:Windows 10  //操作系统的名称
    sun.jnu.encoding:GBK
    java.library.path:D:\oracle-java-1.8-x64\jdk1.8.0_261\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;D:\oracle-java-1.8-x86\jdk1.8.0_202\bin;C:\Users\Marydon\AppData\Local\Microsoft\WindowsApps;.    //加载库时搜索的路径列表
    jboss.modules.system.pkgs:com.intellij.rt
    java.specification.name:Java Platform API Specification //Java 运行时环境规范名称
    java.class.version:52.0 //Java 类格式版本号
    sun.management.compiler:HotSpot 64-Bit Tiered Compilers
    os.version:10.0 //操作系统的版本
    user.home:C:\Users\Marydon  //用户的主目录
    user.timezone:
    java.awt.printerjob:sun.awt.windows.WPrinterJob
    file.encoding:UTF-8
    java.specification.version:1.8  //Java 运行时环境规范版本
    java.class.path:D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\charsets.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\deploy.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\ext\access-bridge-64.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\ext\cldrdata.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\ext\dnsns.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\ext\jaccess.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\ext\jfxrt.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\ext\localedata.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\ext\nashorn.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\ext\sunec.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\ext\sunjce_provider.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\ext\sunmscapi.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\ext\sunpkcs11.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\ext\zipfs.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\javaws.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\jce.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\jfr.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\jfxswt.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\jsse.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\management-agent.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\plugin.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\resources.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\rt.jar;D:\workspace-idea\upload\target\test-classes;D:\workspace-idea\upload\target\classes;C:\Users\Marydon\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.6.2\spring-boot-starter-web-2.6.2.jar;C:\Users\Marydon\.m2\repository\org\springframework\boot\spring-boot-starter\2.6.2\spring-boot-starter-2.6.2.jar;C:\Users\Marydon\.m2\repository\org\springframework\boot\spring-boot\2.6.2\spring-boot-2.6.2.jar;C:\Users\Marydon\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.6.2\spring-boot-starter-logging-2.6.2.jar;C:\Users\Marydon\.m2\repository\ch\qos\logback\logback-classic\1.2.9\logback-classic-1.2.9.jar;C:\Users\Marydon\.m2\repository\ch\qos\logback\logback-core\1.2.9\logback-core-1.2.9.jar;C:\Users\Marydon\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.0\log4j-to-slf4j-2.17.0.jar;C:\Users\Marydon\.m2\repository\org\apache\logging\log4j\log4j-api\2.17.0\log4j-api-2.17.0.jar;C:\Users\Marydon\.m2\repository\org\slf4j\jul-to-slf4j\1.7.32\jul-to-slf4j-1.7.32.jar;C:\Users\Marydon\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\Marydon\.m2\repository\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;C:\Users\Marydon\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.6.2\spring-boot-starter-json-2.6.2.jar;C:\Users\Marydon\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.13.1\jackson-databind-2.13.1.jar;C:\Users\Marydon\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.13.1\jackson-annotations-2.13.1.jar;C:\Users\Marydon\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.13.1\jackson-core-2.13.1.jar;C:\Users\Marydon\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.1\jackson-datatype-jdk8-2.13.1.jar;C:\Users\Marydon\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.1\jackson-datatype-jsr310-2.13.1.jar;C:\Users\Marydon\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.1\jackson-module-parameter-names-2.13.1.jar;C:\Users\Marydon\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.6.2\spring-boot-starter-tomcat-2.6.2.jar;C:\Users\Marydon\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.56\tomcat-embed-core-9.0.56.jar;C:\Users\Marydon\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.56\tomcat-embed-el-9.0.56.jar;C:\Users\Marydon\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.56\tomcat-embed-websocket-9.0.56.jar;C:\Users\Marydon\.m2\repository\org\springframework\spring-web\5.3.14\spring-web-5.3.14.jar;C:\Users\Marydon\.m2\repository\org\springframework\spring-beans\5.3.14\spring-beans-5.3.14.jar;C:\Users\Marydon\.m2\repository\org\springframework\spring-webmvc\5.3.14\spring-webmvc-5.3.14.jar;C:\Users\Marydon\.m2\repository\org\springframework\spring-aop\5.3.14\spring-aop-5.3.14.jar;C:\Users\Marydon\.m2\repository\org\springframework\spring-context\5.3.14\spring-context-5.3.14.jar;C:\Users\Marydon\.m2\repository\org\springframework\spring-expression\5.3.14\spring-expression-5.3.14.jar;C:\Users\Marydon\.m2\repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\2.2.1\mybatis-spring-boot-starter-2.2.1.jar;C:\Users\Marydon\.m2\repository\org\springframework\boot\spring-boot-starter-jdbc\2.6.2\spring-boot-starter-jdbc-2.6.2.jar;C:\Users\Marydon\.m2\repository\com\zaxxer\HikariCP\4.0.3\HikariCP-4.0.3.jar;C:\Users\Marydon\.m2\repository\org\springframework\spring-jdbc\5.3.14\spring-jdbc-5.3.14.jar;C:\Users\Marydon\.m2\repository\org\springframework\spring-tx\5.3.14\spring-tx-5.3.14.jar;C:\Users\Marydon\.m2\repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.2.1\mybatis-spring-boot-autoconfigure-2.2.1.jar;C:\Users\Marydon\.m2\repository\org\mybatis\mybatis\3.5.9\mybatis-3.5.9.jar;C:\Users\Marydon\.m2\repository\org\mybatis\mybatis-spring\2.0.6\mybatis-spring-2.0.6.jar;C:\Users\Marydon\.m2\repository\com\alibaba\druid-spring-boot-starter\1.1.22\druid-spring-boot-starter-1.1.22.jar;C:\Users\Marydon\.m2\repository\com\alibaba\druid\1.1.22\druid-1.1.22.jar;C:\Users\Marydon\.m2\repository\org\slf4j\slf4j-api\1.7.32\slf4j-api-1.7.32.jar;C:\Users\Marydon\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.6.2\spring-boot-autoconfigure-2.6.2.jar;C:\Users\Marydon\.m2\repository\mysql\mysql-connector-java\8.0.27\mysql-connector-java-8.0.27.jar;C:\Users\Marydon\.m2\repository\org\projectlombok\lombok\1.18.22\lombok-1.18.22.jar;C:\Users\Marydon\.m2\repository\org\springframework\boot\spring-boot-starter-test\2.6.2\spring-boot-starter-test-2.6.2.jar;C:\Users\Marydon\.m2\repository\org\springframework\boot\spring-boot-test\2.6.2\spring-boot-test-2.6.2.jar;C:\Users\Marydon\.m2\repository\org\springframework\boot\spring-boot-test-autoconfigure\2.6.2\spring-boot-test-autoconfigure-2.6.2.jar;C:\Users\Marydon\.m2\repository\com\jayway\jsonpath\json-path\2.6.0\json-path-2.6.0.jar;C:\Users\Marydon\.m2\repository\net\minidev\json-smart\2.4.7\json-smart-2.4.7.jar;C:\Users\Marydon\.m2\repository\net\minidev\accessors-smart\2.4.7\accessors-smart-2.4.7.jar;C:\Users\Marydon\.m2\repository\org\ow2\asm\asm\9.1\asm-9.1.jar;C:\Users\Marydon\.m2\repository\jakarta\xml\bind\jakarta.xml.bind-api\2.3.3\jakarta.xml.bind-api-2.3.3.jar;C:\Users\Marydon\.m2\repository\jakarta\activation\jakarta.activation-api\1.2.2\jakarta.activation-api-1.2.2.jar;C:\Users\Marydon\.m2\repository\org\assertj\assertj-core\3.21.0\assertj-core-3.21.0.jar;C:\Users\Marydon\.m2\repository\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar;C:\Users\Marydon\.m2\repository\org\junit\jupiter\junit-jupiter\5.8.2\junit-jupiter-5.8.2.jar;C:\Users\Marydon\.m2\repository\org\junit\jupiter\junit-jupiter-api\5.8.2\junit-jupiter-api-5.8.2.jar;C:\Users\Marydon\.m2\repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;C:\Users\Marydon\.m2\repository\org\junit\platform\junit-platform-commons\1.8.2\junit-platform-commons-1.8.2.jar;C:\Users\Marydon\.m2\repository\org\apiguardian\apiguardian-api\1.1.2\apiguardian-api-1.1.2.jar;C:\Users\Marydon\.m2\repository\org\junit\jupiter\junit-jupiter-params\5.8.2\junit-jupiter-params-5.8.2.jar;C:\Users\Marydon\.m2\repository\org\junit\jupiter\junit-jupiter-engine\5.8.2\junit-jupiter-engine-5.8.2.jar;C:\Users\Marydon\.m2\repository\org\junit\platform\junit-platform-engine\1.8.2\junit-platform-engine-1.8.2.jar;C:\Users\Marydon\.m2\repository\org\mockito\mockito-core\4.0.0\mockito-core-4.0.0.jar;C:\Users\Marydon\.m2\repository\net\bytebuddy\byte-buddy\1.11.22\byte-buddy-1.11.22.jar;C:\Users\Marydon\.m2\repository\net\bytebuddy\byte-buddy-agent\1.11.22\byte-buddy-agent-1.11.22.jar;C:\Users\Marydon\.m2\repository\org\objenesis\objenesis\3.2\objenesis-3.2.jar;C:\Users\Marydon\.m2\repository\org\mockito\mockito-junit-jupiter\4.0.0\mockito-junit-jupiter-4.0.0.jar;C:\Users\Marydon\.m2\repository\org\skyscreamer\jsonassert\1.5.0\jsonassert-1.5.0.jar;C:\Users\Marydon\.m2\repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;C:\Users\Marydon\.m2\repository\org\springframework\spring-core\5.3.14\spring-core-5.3.14.jar;C:\Users\Marydon\.m2\repository\org\springframework\spring-jcl\5.3.14\spring-jcl-5.3.14.jar;C:\Users\Marydon\.m2\repository\org\springframework\spring-test\5.3.14\spring-test-5.3.14.jar;C:\Users\Marydon\.m2\repository\org\xmlunit\xmlunit-core\2.8.4\xmlunit-core-2.8.4.jar;C:\Users\Marydon\.m2\repository\org\hibernate\validator\hibernate-validator\6.0.18.Final\hibernate-validator-6.0.18.Final.jar;C:\Users\Marydon\.m2\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;C:\Users\Marydon\.m2\repository\org\jboss\logging\jboss-logging\3.4.2.Final\jboss-logging-3.4.2.Final.jar;C:\Users\Marydon\.m2\repository\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;D:\idea2020\lib\idea_rt.jar;D:\idea2020\plugins\java\lib\rt\debugger-agent.jar  // Java 类路径
    user.name:Marydon   //用户的账户名称
    java.vm.specification.version:1.8   // Java 虚拟机规范版本
    sun.java.command:com.example.upload.Test
    java.home:D:\oracle-java-1.8-x64\jdk1.8.0_261\jre   //Java 安装目录
    sun.arch.data.model:64
    user.language:zh
    java.specification.vendor:Oracle Corporation    //Java 运行时环境规范供应商
    awt.toolkit:sun.awt.windows.WToolkit
    java.vm.info:mixed mode
    java.version:1.8.0_261  //Java 运行时环境版本
    java.ext.dirs:D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext   //一个或多个扩展目录的路径
    sun.boot.class.path:D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\resources.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\rt.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\sunrsasign.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\jsse.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\jce.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\charsets.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\lib\jfr.jar;D:\oracle-java-1.8-x64\jdk1.8.0_261\jre\classes
    java.vendor:Oracle Corporation  //Java 运行时环境供应商
    file.separator:\    //文件分隔符(在 UNIX 系统中是"/")
    java.vendor.url.bug:http://bugreport.sun.com/bugreport/
    sun.io.unicode.encoding:UnicodeLittle
    sun.cpu.endian:little
    sun.desktop:windows
    sun.cpu.isalist:amd64
    

    如何获取操作系统环境变量?

    通过System.getenv("key")获取;

    遍历操作系统环境变量:

    System.getenv().forEach((k, v) -> {
        System.out.println(k + "=" + v);
    });

    操作系统配置的环境变量有:

    查看代码
    
    USERDOMAIN_ROAMINGPROFILE=LENVON
    LOCALAPPDATA=C:\Users\Marydon\AppData\Local
    PROCESSOR_LEVEL=6
    USERDOMAIN=LENVON
    FPS_BROWSER_APP_PROFILE_STRING=Internet Explorer
    LOGONSERVER=\\LENVON
    JAVA_HOME=D:\oracle-java-1.8-x86\jdk1.8.0_202
    SESSIONNAME=Console
    ALLUSERSPROFILE=C:\ProgramData
    INTEL_DEV_REDIST=C:\Program Files (x86)\Common Files\Intel\Shared Libraries\
    PROCESSOR_ARCHITECTURE=AMD64
    MIC_LD_LIBRARY_PATH=C:\Program Files (x86)\Common Files\Intel\Shared Libraries\compiler\lib\mic
    PSModulePath=C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
    SystemDrive=C:
    APPDATA=C:\Users\Marydon\AppData\Roaming
    USERNAME=Marydon
    ProgramFiles(x86)=C:\Program Files (x86)
    CommonProgramFiles=C:\Program Files\Common Files
    Path=C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;D:\oracle-java-1.8-x86\jdk1.8.0_202\bin;C:\Users\Marydon\AppData\Local\Microsoft\WindowsApps
    FPS_BROWSER_USER_PROFILE_STRING=Default
    PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
    DriverData=C:\Windows\System32\Drivers\DriverData
    OS=Windows_NT
    COMPUTERNAME=LENVON
    PROCESSOR_REVISION=8e0a
    CommonProgramW6432=C:\Program Files\Common Files
    NLS_LANG=AMERICAN_AMERICA.AL32UTF8
    ComSpec=C:\WINDOWS\system32\cmd.exe
    ProgramData=C:\ProgramData
    ProgramW6432=C:\Program Files
    HOMEPATH=\Users\Marydon
    SystemRoot=C:\WINDOWS
    TEMP=C:\Users\Marydon\AppData\Local\Temp
    HOMEDRIVE=C:
    NLS_CHARACTERSET=AL32UTF8
    PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 142 Stepping 10, GenuineIntel
    USERPROFILE=C:\Users\Marydon
    TMP=C:\Users\Marydon\AppData\Local\Temp
    CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
    ProgramFiles=C:\Program Files
    PUBLIC=C:\Users\Public
    NUMBER_OF_PROCESSORS=8
    windir=C:\WINDOWS
    =::=::\
    IDEA_INITIAL_DIRECTORY=D:\idea2020\bin

     

    写在最后

      哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!

     相关推荐:

  • 相关阅读:
    动态规划:POJ2576-Tug of War(二维费用的背包问题)
    动态规划:HDU3496-Watch The Movie(二维费用的背包问题)
    动态规划:HDU1712-ACboy needs your help(分组背包问题)
    水题:HDU1303-Doubles
    动态规划:HDU2844-Coins(多重背包的二进制优化)
    动态规划:HDU1059-Dividing(多重背包问题的二进制优化)
    动态规划:HDU1224-Free DIY Tour
    动态规划:HDU1864-最大报销额(处理带小数的dp问题)
    红黑树
    二叉树
  • 原文地址:https://www.cnblogs.com/Marydon20170307/p/15798356.html
Copyright © 2020-2023  润新知