System.getProperty方法

2022/9/1 6:53:06

本文主要是介绍System.getProperty方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Demo

private static String zookeeperHost = System.getProperty("zookeeper.address", "127.0.0.1");
private static String zookeeperPort = System.getProperty("zookeeper.port", "2181");

使用System.getProperty获得属性,如果未获得指定key的值,使用后面的作为default value。

 

System.getProperty方法支持以下属性:

  • java.version:Java Runtime Environment version, which may be interpreted as a Runtime.Version
  • java.version.date:Java Runtime Environment version date, in ISO-8601 YYYY-MM-DD format, which may be interpreted as a java.time.LocalDate
  • java.vendor:Java Runtime Environment vendor
  • java.vendor.url:Java vendor URL
  • java.vendor.version:Java vendor version
  • java.home:Java installation directory
  • java.vm.specification.version:Java Virtual Machine specification version, whose value is the feature element of the runtime 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 which may be interpreted as a Runtime.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, whose value is the feature element of the runtime 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 (refer to ClassLoader.getSystemClassLoader() for details)
  • 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
  • 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
    public static String getProperty(String key) {
        checkKey(key);
        SecurityManager sm = getSecurityManager();
        if (sm != null) {
            sm.checkPropertyAccess(key);
        }

        return props.getProperty(key);
    }

  

 checkKey(String key)

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");
        }
    }
props.getProperty(key)
    public String getProperty(String key) {
        Object oval = map.get(key);
        String sval = (oval instanceof String) ? (String)oval : null;
        Properties defaults;
        return ((sval == null) && ((defaults = this.defaults) != null)) ? defaults.getProperty(key) : sval;
    }

 



这篇关于System.getProperty方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程