Class SystemUtils


  • public final class SystemUtils
    extends java.lang.Object
    JDK 9 support.
    Since:
    2.20.1
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private SystemUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean endsWithJavaPath​(java.lang.String jvmExecPath)  
      private static java.math.BigDecimal getJavaSpecificationVersion()
      Safely extracts major and minor version as fractional number from
      static boolean isBuiltInJava9AtLeast()  
      static boolean isJava9AtLeast​(java.lang.String jvmExecutablePath)  
      static boolean isJava9AtLeast​(java.math.BigDecimal version)  
      static java.lang.Long pid()  
      (package private) static java.lang.Long pidOnJava9()  
      (package private) static java.lang.Long pidOnJMX()  
      (package private) static java.lang.Long pidStatusOnBSD()
      The process status.
      (package private) static java.lang.Long pidStatusOnBSD​(java.lang.String root)
      For testing purposes only.
      (package private) static java.lang.Long pidStatusOnLinux()
      $ cat /proc/self/stat
      48982 (cat) R 9744 48982 9744 34818 48982 8192 185 0 0 0 0 0 0 0 20 0 1 0 137436614 103354368 134 18446744073709551615 4194304 4235780 140737488346592 140737488343784 252896458544 0 0 0 0 0 0 0 17 2 0 0 0 0 0
      $ SELF_PID=$(cat /proc/self/stat)
      $ echo $CPU_ID | gawk '{print $1}'
      48982
      (package private) static java.lang.Long pidStatusOnLinux​(java.lang.String root)
      For testing purposes only.
      static java.lang.ClassLoader platformClassLoader()  
      (package private) static java.lang.ClassLoader reflectClassLoader​(java.lang.Class<?> target, java.lang.String getterMethodName)  
      static java.io.File toJdkHomeFromJre()
      If system property java.home is /jdk (since jdk9) or /jdk/jre (prior to jdk9) then the absolute path to JDK home is returned /jdk.
      (package private) static java.io.File toJdkHomeFromJre​(java.lang.String jreHome)
      If jreHome is /jdk (since jdk9) or /jdk/jre (prior to jdk9) then the absolute path to JDK home is returned /jdk.
      static java.io.File toJdkHomeFromJvmExec​(java.lang.String jvmExecutable)
      If jvmExecutable is /jdk/bin/java (since jdk9) or /jdk/jre/bin/java (prior to jdk9) then the absolute path to JDK home is returned /jdk.
      static java.math.BigDecimal toJdkVersionFromReleaseFile​(java.io.File jdkHome)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • JAVA_SPECIFICATION_VERSION

        public static final java.math.BigDecimal JAVA_SPECIFICATION_VERSION
      • JIGSAW_JAVA_VERSION

        private static final java.math.BigDecimal JIGSAW_JAVA_VERSION
      • PROC_STATUS_PID_FIRST_CHARS

        private static final int PROC_STATUS_PID_FIRST_CHARS
        See Also:
        Constant Field Values
    • Constructor Detail

      • SystemUtils

        private SystemUtils()
    • Method Detail

      • endsWithJavaPath

        public static boolean endsWithJavaPath​(java.lang.String jvmExecPath)
        Parameters:
        jvmExecPath - e.g. /jdk/bin/java, /jdk/jre/bin/java
        Returns:
        true if jvmExecPath is path to java binary executor
      • toJdkHomeFromJvmExec

        public static java.io.File toJdkHomeFromJvmExec​(java.lang.String jvmExecutable)
        If jvmExecutable is /jdk/bin/java (since jdk9) or /jdk/jre/bin/java (prior to jdk9) then the absolute path to JDK home is returned /jdk.
        Null is returned if jvmExecutable is incorrect.
        Parameters:
        jvmExecutable - /jdk/bin/java* or /jdk/jre/bin/java*
        Returns:
        path to jdk directory; or null if wrong path or directory layout of JDK installation.
      • toJdkHomeFromJre

        public static java.io.File toJdkHomeFromJre()
        If system property java.home is /jdk (since jdk9) or /jdk/jre (prior to jdk9) then the absolute path to JDK home is returned /jdk.
        Returns:
        path to JDK
      • toJdkHomeFromJre

        static java.io.File toJdkHomeFromJre​(java.lang.String jreHome)
        If jreHome is /jdk (since jdk9) or /jdk/jre (prior to jdk9) then the absolute path to JDK home is returned /jdk.
        JRE home directory jreHome must be taken from system property java.home.
        Parameters:
        jreHome - path to /jdk or /jdk/jre
        Returns:
        path to JDK
      • toJdkVersionFromReleaseFile

        public static java.math.BigDecimal toJdkVersionFromReleaseFile​(java.io.File jdkHome)
      • getJavaSpecificationVersion

        private static java.math.BigDecimal getJavaSpecificationVersion()
        Safely extracts major and minor version as fractional number from
             $MAJOR.$MINOR.$SECURITY
         
        .
        The security version is usually not needed to know. It can be applied to not certified JRE.
        Returns:
        major.minor version derived from java specification version of this JVM, e.g. 1.8, 9, etc.
      • isJava9AtLeast

        public static boolean isJava9AtLeast​(java.lang.String jvmExecutablePath)
      • isBuiltInJava9AtLeast

        public static boolean isBuiltInJava9AtLeast()
      • isJava9AtLeast

        public static boolean isJava9AtLeast​(java.math.BigDecimal version)
      • platformClassLoader

        public static java.lang.ClassLoader platformClassLoader()
      • pid

        public static java.lang.Long pid()
      • pidOnJMX

        static java.lang.Long pidOnJMX()
      • pidStatusOnLinux

        static java.lang.Long pidStatusOnLinux()
                                        throws java.lang.Exception
        $ cat /proc/self/stat
        48982 (cat) R 9744 48982 9744 34818 48982 8192 185 0 0 0 0 0 0 0 20 0 1 0 137436614 103354368 134 18446744073709551615 4194304 4235780 140737488346592 140737488343784 252896458544 0 0 0 0 0 0 0 17 2 0 0 0 0 0
        $ SELF_PID=$(cat /proc/self/stat)
        $ echo $CPU_ID | gawk '{print $1}'
        48982
        Returns:
        self PID
        Throws:
        java.lang.Exception - i/o and number format exc
      • pidStatusOnLinux

        static java.lang.Long pidStatusOnLinux​(java.lang.String root)
                                        throws java.lang.Exception
        For testing purposes only.
        Parameters:
        root - shifted to test-classes
        Returns:
        same as in pidStatusOnLinux()
        Throws:
        java.lang.Exception - same as in pidStatusOnLinux()
      • pidStatusOnBSD

        static java.lang.Long pidStatusOnBSD()
                                      throws java.lang.Exception
        The process status. This file is read-only and returns a single line containing multiple space-separated fields.
        See procfs status
        # cat /proc/curproc/status
        cat 60424 60386 60424 60386 5,0 ctty 972854153,236415 0,0 0,1043 nochan 0 0 0,0 prisoner
        Fields are:
        comm pid ppid pgid sid maj, min ctty, sldr start user/system time wmsg euid ruid rgid,egid, groups[1 .. NGROUPS] hostname
        Returns:
        current PID
        Throws:
        java.lang.Exception - if could not read /proc/curproc/status
      • pidStatusOnBSD

        static java.lang.Long pidStatusOnBSD​(java.lang.String root)
                                      throws java.lang.Exception
        For testing purposes only.
        Parameters:
        root - shifted to test-classes
        Returns:
        same as in pidStatusOnBSD()
        Throws:
        java.lang.Exception - same as in pidStatusOnBSD()
      • pidOnJava9

        static java.lang.Long pidOnJava9()
      • reflectClassLoader

        static java.lang.ClassLoader reflectClassLoader​(java.lang.Class<?> target,
                                                        java.lang.String getterMethodName)