java获取系统相关信息(来自于gecko的SystemUtils,RemotiingUtils工具类)

  • Post author:
  • Post category:java



1 判断是否为linux系统


public static final String OS_NAME = System.getProperty("os.name");
        if (OS_NAME != null && OS_NAME.toLowerCase().indexOf("linux") >= 0) { 
            isLinuxPlatform = true; 
        }



2 获取cpu个数





   /**
     * 默认为CPU个数-1,留一个CPU做网卡中断
     * 
     * @return
     */
    public static int getSystemThreadCount() {
        final int cpus = getCpuProcessorCount();
        final int result = cpus - 1;
        return result == 0 ? 1 : result;
    }


    public static int getCpuProcessorCount() {
        return Runtime.getRuntime().availableProcessors();
    }


3 获取jdk版本信息


 public static final String JAVA_VERSION = System.getProperty("java.version");
    private static boolean isAfterJava6u4Version = false;
    static {
        if (JAVA_VERSION != null) {
            // java4 or java5
            if (JAVA_VERSION.indexOf("1.4.") >= 0 || JAVA_VERSION.indexOf("1.5.") >= 0) {
                isAfterJava6u4Version = false;
            }
            else if (JAVA_VERSION.indexOf("1.6.") >= 0) {
                final int index = JAVA_VERSION.indexOf("_");
                if (index > 0) {
                    final String subVersionStr = JAVA_VERSION.substring(index + 1);
                    if (subVersionStr != null && subVersionStr.length() > 0) {
                        try {
                            final int subVersion = Integer.parseInt(subVersionStr);
                            if (subVersion >= 4) {
                                isAfterJava6u4Version = true;
                            }
                        }
                        catch (final Exception e) {

                        }
                    }
                }
                // after java6
            }
            else {
                isAfterJava6u4Version = true;
            }
        }
    }



4 获取本机ip地址





 // 遍历网卡,查找一个非回路ip地址并返回,如果没有找到,则返回InetAddress.getLocalHost()
    public static InetAddress getLocalHostAddress() throws UnknownHostException, SocketException {
        final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
        InetAddress ipv6Address = null;
        while (enumeration.hasMoreElements()) {
            final NetworkInterface networkInterface = enumeration.nextElement();
            final Enumeration<InetAddress> en = networkInterface.getInetAddresses();
            while (en.hasMoreElements()) {
                final InetAddress address = en.nextElement();
                if (!address.isLoopbackAddress()) {
                    if (address instanceof Inet6Address) {
                        ipv6Address = address;
                    }
                    else {
                        // 优先使用ipv4
                        return address;
                    }
                }
            }

        }
        // 没有ipv4,则使用ipv6
        if (ipv6Address != null) {
            return ipv6Address;
        }
        return InetAddress.getLocalHost();
    }