`
MouseLearnJava
  • 浏览: 460673 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

获取System Properties的方法

    博客分类:
  • Java
阅读更多

我们可以通过如下两种方式获取System Properties信息:

1. System.getProperties()
2. ManagementFactory.getRuntimeMXBean().getSystemProperties()


ManagementFactory.getRuntimeMXBean().getSystemProperties方法采用System.getProperties()来完成的,如果在本地运行程序去获取某些系统属性的值,结果是一样的。

它们的一个区别,RuntimeMXBean能够去获取远程 VM上的System Properties

ManagementFactory的newPlatformMXBeanProxy方法提供了访问远程VM的方法。可采取如下方式来访问远程VM,参考https://blogs.oracle.com/jmxetc/entry/how_to_retrieve_remote_jvm

      
// Connect to target 
        final JMXConnector connector = JMXConnectorFactory.connect(target);
        
        // Get an MBeanServerConnection on the remote VM.
        final MBeanServerConnection remote = 
                connector.getMBeanServerConnection();
        
        final RuntimeMXBean remoteRuntime = 
                ManagementFactory.newPlatformMXBeanProxy(
                    remote,
                    ManagementFactory.RUNTIME_MXBEAN_NAME,
                    RuntimeMXBean.class);




RuntimeMXBean的具体内容如下:

/*
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package sun.management;

import java.lang.management.RuntimeMXBean;

import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Properties;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import javax.management.openmbean.OpenDataException;

/**
 * Implementation class for the runtime subsystem.
 * Standard and committed hotspot-specific metrics if any.
 *
 * ManagementFactory.getRuntimeMXBean() returns an instance
 * of this class.
 */
class RuntimeImpl implements RuntimeMXBean {

    private final VMManagement jvm;
    private final long vmStartupTime;

    /**
     * Constructor of RuntimeImpl class.
     */
    RuntimeImpl(VMManagement vm) {
        this.jvm = vm;
        this.vmStartupTime = jvm.getStartupTime();
    }

    public String getName() {
        return jvm.getVmId();
    }

    public String getManagementSpecVersion() {
        return jvm.getManagementVersion();
    }

    public String getVmName() {
        return jvm.getVmName();
    }

    public String getVmVendor() {
        return jvm.getVmVendor();
    } 

    public String getVmVersion() {
        return jvm.getVmVersion();
    }

    public String getSpecName() {
        return jvm.getVmSpecName();
    }

    public String getSpecVendor() {
        return jvm.getVmSpecVendor();
    }

    public String getSpecVersion() {
        return jvm.getVmSpecVersion();
    }

    public String getClassPath() {
        return jvm.getClassPath();
    }

    public String getLibraryPath() {
        return jvm.getLibraryPath();
    }

    public String getBootClassPath() {
        if (!isBootClassPathSupported()) {
            throw new UnsupportedOperationException(
                "Boot class path mechanism is not supported");
        }
        ManagementFactory.checkMonitorAccess();
        return jvm.getBootClassPath();
    }

    public List<String> getInputArguments() {
        ManagementFactory.checkMonitorAccess();
        return jvm.getVmArguments();
    }

    public long getUptime() {       
        long current = System.currentTimeMillis();

        // TODO: If called from client side when we support
        // MBean proxy to read performance counters from shared memory, 
        // need to check if the monitored VM exitd.
        return (current - vmStartupTime);
    }

    public long getStartTime() {
        return vmStartupTime;
    }

    public boolean isBootClassPathSupported() {
        return jvm.isBootClassPathSupported();
    }

    public Map<String,String> getSystemProperties() {
        Properties sysProps = System.getProperties();
        Map<String,String> map = new HashMap<String, String>();

        // Properties.entrySet() does not include the entries in
        // the default properties.  So use Properties.stringPropertyNames()
        // to get the list of property keys including the default ones.
        Set<String> keys = sysProps.stringPropertyNames();
        for (String k : keys) {
            String value = sysProps.getProperty(k);
            map.put(k, value);
        }

        return map;
    }
}



如果博友知道两者的其它区别,也请指出,谢谢。
0
7
分享到:
评论
1 楼 东方胜 2013-08-19  

相关推荐

    获取Android手机或平板唯一识别号(imei或meid).rar

    获取Android手机或平板当前系统版本号、型号、厂商、唯一识别号(imei或meid)等

    C# 获取U盘ID序列号及U盘信息

    获取U盘ID序列号 VS2005编译通过,源码源自CSDN。已经测试好用。 可以获得U盘名称,制造商ID号 版本号 U盘序列号及容量 Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; ...

    Android 应用获取SystmeProperity的两种方式

    非系统应用通过两种方式获取系统SystemProperity的属性。Java层和Native层

    MiTeC System Information Component Suite 11.3.0 For D7 破解

    这是获取硬件信息的 Delphi 第三方控件包,非常强大。 官方在2013年7月24日更新的版本,试用版在软件运行时会弹出控件版权信息框。破解MSI_Common.dcu后,无信息框弹出! 控件详细说明: Product: MiTeC System ...

    Android NDK 获取手机部分信息 build.prop

    下面是通过 adb shell cat /system/build.prop 获取到的信息 # begin build properties开始设置系统性能 # autogenerated by buildinfo.sh{通过设置形成系统信息} ro.build.id=MIUI(版本ID) ro.build.display.id=...

    BankingSystem:用C ++编程的简单银行系统

    银行系统 银行系统是一个简单的C ++程序,在典型的银行系统中具有3种类型的用户:客户,经理,维护人员 此应用程序是为Western的面向对象设计课程开发的。... 获取银行帐户统计信息(最大,最小,平均

    C#编程获取客户端计算机硬件及系统信息功能示例

    本文实例讲述了C#编程获取客户端计算机硬件及系统信息功能。分享给大家供大家参考,具体如下: 这里使用C#获取客户端计算机硬件及系统信息 ,包括CPU、硬盘、IP、MAC地址、操作系统等。 1、项目引用System....

    跨项目调用webservice接口

    *这里是我的参数放在了properties文件中,我在读取里面的参数,这里我们也可以通过方法传参数 *如 : testWebService(String url,String xMlStr)() 那么在调用的时候就可以直接传进来了 *url 是你访问的webservice 的...

    java解析给定url

    System.out.println("读取配置文件/config.properties出错"); } } /** * 程序总入口 */ private void start() { for(int i = 0; i (); i++) { URLConnection con = getConnection(configList.get(i)); ...

    上传并读取EXCEL的数据,显示到GridView中

    System.Data.DataTable sTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null); string sheetName = string.Empty; List&lt;string&gt; siteCodes = new List(); string ...

    JDBC笔记.docx

    二、JDBC接口 所属包:java.sql Javax.sql ... //调用数据库连接方法,获取数据库连接 Connection conn = driver.connect(url, pro); System.out.println(conn); //关闭数据库连接 conn.close(); }

    c#读写App.config,ConfigurationManager.AppSettings 不生效的解决方法

    你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了App.config配置文件。 本文探讨用代码的方式访问 App.config 的方法。关于 App.config 的使用远比上面提到的用途...

    发送邮件发送,先提一下SMTP

    再简单介绍一下名称空间(NameSpace)System.Web.Mail类库里所提供的邮件发送的对象、属性和方法 (1)它有三个类:SmtpMail、MailMessage和MailAttachment。这三个对象本文的示例程序代码中都应用到了! 1. ...

    config4j

    //--获取ccc 结点的 attributes 值 Map obj3 = (Map)cb.getRoot().getValueByPath("servers.dbserver.ccc:$properties"); //--获取dbserver 结点的 孩子结点表 List&lt;ConfigNode&gt; ...

    system-ane:移动系统 AIR 原生扩展

    获取设备标识符 发送消息(日志、通知) 监听传感器 先决条件 JDK(通用) 设置JAVA_HOME环境。 变量是必不可少的 Gradle(普通) 设置GRADLE_HOME环境。 变量是必不可少的 Adobe AIR SDK (Flash) 设置AIR_HOME...

    parts-number-management-system:针对美国仓库实施的基于Web的零件编号管理解决方案。 该解决方案可帮助他们管理和跟踪零件生产,进度等。请查看自述文件以获取完整说明。

    零件编号管理系统 这是为美国仓库实施的基于Web的零件编号... 请查看\ src \ main \ resources \ application.properties 根据您的LDAP服务器更新LDAP身份验证设置。 请查看\ src \ main \ resources \ global.propert

    超级有影响力霸气的Java面试题大全文档

    redirect就是服务端根据逻辑,发送一个状态码,告诉浏览器重新去请求那个地址,一般来说浏览器会用刚才请求的所有参数重新请求,所以session,request参数都可以获取。 23、EJB与JAVA BEAN的区别?  Java Bean 是可...

    JDBC中连接MySQL需要的jar包,包括Druid(德鲁伊)数据源

    Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCP、Proxool等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL... //测试获取超过最大连接数的连接数量(连接关闭、未关闭的情况) }

    玩转模板--自动代码生成工程

    system * @author $!author * @version $!version * @date $!dateTool.format('yyyy-MM-dd', ${date}) */ public class $!{tableAlias}{ #foreach($item in $columnList) private $!item.data_type $!item....

Global site tag (gtag.js) - Google Analytics