关于java系统变量获取源码

获取系统变量方式

public class DemoClasspath {
  public static void main(String[] args) {
    String[] classpath =
        System.getProperty("java.class.path").split(System.getProperty("path.separator"));
    for (String path : classpath) {
      System.out.println(path);
    }
  }
}

结果中包含jre/lib下jar包、项目打包之后classes下文件、mavan配置依赖的jar包

C:\soft\Java\jdk1.8.0_221\jre\lib\charsets.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\deploy.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\ext\access-bridge-64.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\ext\cldrdata.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\ext\dnsns.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\ext\jaccess.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\ext\jfxrt.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\ext\localedata.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\ext\nashorn.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\ext\sunec.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\ext\sunjce_provider.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\ext\sunmscapi.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\ext\sunpkcs11.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\ext\zipfs.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\javaws.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\jce.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\jfr.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\jfxswt.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\jsse.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\management-agent.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\plugin.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\resources.jar
C:\soft\Java\jdk1.8.0_221\jre\lib\rt.jar
C:\workspace-eclipse\demo\target\classes
C:\repository\javax\servlet\jstl\1.2\jstl-1.2.jar
C:\repository\com\alibaba\fastjson\2.0.25\fastjson-2.0.25.jar
C:\repository\com\alibaba\fastjson2\fastjson2-extension\2.0.25\fastjson2-extension-2.0.25.jar
C:\repository\com\alibaba\fastjson2\fastjson2\2.0.25\fastjson2-2.0.25.jar
C:\repository\com\alibaba\druid\1.2.16\druid-1.2.16.jar

查看源码,寻找获取方式。native方法,需要查看jvm源码。

 public static void setProperties(Properties props) {
        SecurityManager sm = getSecurityManager();
        if (sm != null) {
            sm.checkPropertiesAccess();
        }
        if (props == null) {
            props = new Properties();
            initProperties(props);
        }
        System.props = props;
    }

setProperties调用initProperties方法,如下所示:
private static native Properties initProperties(Properties props);

额外所有属性预先定义key值:

/**
System properties. The following properties are guaranteed to be defined:
java.version
Java version number
java.vendor
Java vendor specific string
java.vendor.url
Java vendor URL
java.home
Java installation directory
java.class.version
Java class version number
java.class.path
Java classpath
os.name
Operating System Name
os.arch
Operating System Architecture
os.version
Operating System Version
file.separator
File separator ("/" on Unix)
path.separator
Path separator (":" on Unix)
line.separator
Line separator ("\n" on Unix)
user.name
User account name
user.home
User home directory
user.dir
User's current working directory
*/

所有的key值

Determines the current system properties.
First, if there is a security manager, its checkPropertiesAccess method is called with no arguments. This may result in a security exception.
The current set of system properties for use by the getProperty(String) method is returned as a Properties object. If there is no current set of system properties, a set of system properties is first created and initialized. This set of system properties always includes values for the following keys:
Key
Description of Associated Value
java.version
Java Runtime Environment version
java.vendor
Java Runtime Environment vendor
java.vendor.url
Java vendor URL
java.home
Java installation directory
java.vm.specification.version
Java Virtual Machine specification version
java.vm.specification.vendor
Java Virtual Machine specification vendor
java.vm.specification.name
Java Virtual Machine specification name
java.vm.version
Java Virtual Machine implementation version
java.vm.vendor
Java Virtual Machine implementation vendor
java.vm.name
Java Virtual Machine implementation name
java.specification.version
Java Runtime Environment specification version
java.specification.vendor
Java Runtime Environment specification vendor
java.specification.name
Java Runtime Environment specification name
java.class.version
Java class format version number
java.class.path
Java class path
java.library.path
List of paths to search when loading libraries
java.io.tmpdir
Default temp file path
java.compiler
Name of JIT compiler to use
java.ext.dirs
Path of extension directory or directories Deprecated. This property, and the mechanism which implements it, may be removed in a future release.
os.name
Operating system name
os.arch
Operating system architecture
os.version
Operating system version
file.separator
File separator ("/" on UNIX)
path.separator
Path separator (":" on UNIX)
line.separator
Line separator ("\n" on UNIX)
user.name
User's account name
user.home
User's home directory
user.dir
User's current working directory
Multiple paths in a system property value are separated by the path separator character of the platform.
Note that even if the security manager does not permit the getProperties operation, it may choose to permit the getProperty(String) operation.
Returns:
the system properties
Throws:
SecurityExceptionif a security manager exists and its checkPropertiesAccess method doesn't allow access to the system properties.
See Also:
setProperties, SecurityException, SecurityManager.checkPropertiesAccess(), Properties
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值