`
Dead_knight
  • 浏览: 1194667 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
博客专栏
752c8642-b795-3fe6-946e-a4e845bffdec
Spring Securi...
浏览量:238447
33caa84e-18a6-3036-a82b-6e2106a4de63
clojure专题
浏览量:48167
E17ca077-44df-3816-a3fe-471c43f6e1e5
WebLogic11g
浏览量:236014
社区版块
存档分类
最新评论

获取系统属性值

阅读更多
一、通过System.getProperties()获取系统常用属性:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.System;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.StringTokenizer;

public class SystemInfo
{
private static Properties _property;


public static void main(String[] a)
{
_property=System.getProperties();

Hashtable hashKey;
hashKey=new Hashtable();
hashKey.put("java.home", "Java安装目录");
hashKey.put("java.class.path", "装载类的路径");
hashKey.put("java.specification.version", "Java API 规范的版本");
hashKey.put("java.specification.vendor", "Java API 规范的厂商");
hashKey.put("java.specification.name", "Java API 规范的名称");
hashKey.put("java.version", "Java API 实现的版本");
hashKey.put("java.vendor", "Java API 实现的厂商");
hashKey.put("java.vendor.url", "Java API 规范厂商的URL");
hashKey.put("java.vm.specification.version", "Java虚拟机规范的版本");
hashKey.put("java.vm.specification.vendor", "Java虚拟机规范的厂商");
hashKey.put("java.vm.specification.name", "Java虚拟机规范的名称");

hashKey.put("java.vm.version", "Java虚拟机实现的版本");
hashKey.put("java.vm.vendor", "Java虚拟机实现的厂商");
hashKey.put("java.vm.name", "Java虚拟机实现的名称");
hashKey.put("java.class.version", "Java类文件格式的版本");
hashKey.put("os.name", "主机操作系统的名称");
hashKey.put("os.arch", "主机操作系统的体系结构");
hashKey.put("os.version", "主机操作系统的版本");
hashKey.put("file.separator", "平台目录的分隔符");
hashKey.put("path.separator", "平台路径的分隔符");
hashKey.put("line.separator", "Java虚拟机规范的厂商");
hashKey.put("user.name", "当前用户的帐户名称");
hashKey.put("user.home", "当前用户的根目录");
hashKey.put("user.dir", "当前工作目录");
Enumeration enum = hashKey.keys();
String propertyKey;
while(enum.hasMoreElements())
{
propertyKey=(String)enum.nextElement();
System.out.println((String)hashKey.get(propertyKey)+":"+_property.getProperty(propertyKey));
}
System.out.println(getComputer());
System.out.println(getIpAddress());
}

    /**
    获取电脑名称
     */
public static String getComputer()
{
String hostName="" ,ip = "";
try
{
ip = InetAddress.getLocalHost().toString();
StringTokenizer tok = new StringTokenizer(ip, "/");
hostName = tok.nextToken();
}
catch (Exception e)
{
e.printStackTrace();
}
return hostName;
}

    /**
    获取IP地址
     */
public static String getIpAddress()
{
String hostName = "";
String ip = "";
try
    {
ip = InetAddress.getLocalHost().toString();
StringTokenizer tok = new StringTokenizer(ip, "/");
hostName = tok.nextToken();
ip = tok.nextToken();
    }
catch (Exception e)
{
e.printStackTrace();
}
    return ip;

}

}

二、由于需求要求获取unix环境下的进程信息,故需要使用Runtime来解决,方案如下:
1.通过编写unix中的shell脚本,如monitor.sh内容:
#!/bin/bash
log_home=/export/home/bea/imep/monitor/imep.log
date +"%Y-%m-%d-%H:%M:%S" > $log_home
ps -ef | grep java >> $log_home
该脚本用于将ps -ef | grep java(当前系统java进程信息)打印的内容重定向至/export/home/bea/imep/monitor/imep.log文件下

2.使用JAVA的Runtime类执行该shell脚本
public void runUnixShell()
{
try
{
Process process = Runtime.getRuntime().exec(“/export/home/bea/imep/monitor/monitor.sh”);  
process.waitFor();
}catch(Exception e)
{
e.printStackTrace();
}
}
而后对此日志文件export/home/bea/imep/monitor/imep.log
进行解析。

3.方法很多,可以更深入研究(可以直接解析unix自动生成的所有进程日志内容,或者通过Process类进行处理……)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics