Class Java

  • Direct Known Subclasses:
    JConsole, JDeps, JMap, JVisualVM, Keytool

    public class Java
    extends Object
    A helper class for Java (JRE/JDK) related things.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Field Detail

      • CLASSPATH_FULL

        protected static String CLASSPATH_FULL
        the cleaned up, full classpath.
      • CLASSPATH_SHORT

        protected static String CLASSPATH_SHORT
        the shortened classpath.
      • MAJOR_VERSION

        protected static Integer MAJOR_VERSION
        the major Java version.
    • Constructor Detail

      • Java

        public Java()
    • Method Detail

      • getMajorVersion

        public static int getMajorVersion()
        Returns the major version of Java.
        Returns:
        the major version, -1 if failed to determine
      • getHome

        public static String getHome()
        Returns the Java home directory of the current JVM.
        Returns:
        the home directory
      • isJRE

        public static boolean isJRE()
        Checks whether Java home is pointing to a JRE instead of a JDK. It is checking for the javac executable.
        Returns:
        true if the JVM is just a JRE
        See Also:
        isJRE(String)
      • isJRE

        public static boolean isJRE​(String path)
        Checks whether directory is pointing to a JRE instead of a JDK. It is checking for the javac executable.
        Parameters:
        path - the path above the "bin" directory
        Returns:
        true if the JVM is just a JRE
        See Also:
        JAVAC
      • isJDK

        public static boolean isJDK()
        Checks whether Java home is representing a JDK. If the directory is JRE, it is checked whether it is just the JRE of a JDK.
        Returns:
        true if the JVM is a JDK
        See Also:
        isJDK(String)
      • isJDK

        public static boolean isJDK​(String path)
        Checks whether directory is representing a JDK. If the directory is JRE, it is checked whether it is just the JRE of a JDK.
        Parameters:
        path - the path above the "bin" directory
        Returns:
        true if the JVM is a JDK
        See Also:
        JAVAC
      • getBinDir

        public static String getBinDir()
        Returns the "bin" directory of the Java home. In case of a JRE of a JDK, this returns the JDK/bin directory and not the JRE/bin one.
        Returns:
        the absolute path to "bin" (incl.)
        See Also:
        getBinDir(String)
      • getBinDir

        public static String getBinDir​(String path)
        Returns the "bin" directory. In case of a JRE of a JDK, this returns the JDK/bin directory and not the JRE/bin one.
        Parameters:
        path - above the "bin"
        Returns:
        the absolute path to "bin" (incl.)
        See Also:
        BIN_DIR
      • getJavaExecutable

        public static String getJavaExecutable()
        Returns the java executable.
        Returns:
        the full path to the java executable
        See Also:
        JAVA, getBinDir()
      • execute

        protected static String execute​(String executable,
                                        String options)
        Executes the executable and returns the output.
        Parameters:
        executable - the jvisualvm executable to use
        options - additional options for jvisualvm
        Returns:
        the output
      • getClassPath

        public static String getClassPath​(boolean shorten)
        Returns the classpath of the current JVM. Does not check whether the directories or jars actually exist, but removes "fake" jars that occur on Mac OSX when unzipping a ZIP file containing jars (these files start with "._").
        Parameters:
        shorten - if true then the paths get removed from the jars
        Returns:
        the classpath
      • main

        public static void main​(String[] args)
        For testing only.
        Parameters:
        args - each argument is interpreted as a directory to test for JRE/JDK/etc.