Class JavaVM


  • public final class JavaVM
    extends Object
    Helper class to locate a JDK by Java major version and return the path to the java executable.
    • Field Detail

      • MAX_JAVA_VERSION_TO_CHECK

        public static final int MAX_JAVA_VERSION_TO_CHECK
        See Also:
        Constant Field Values
    • Method Detail

      • findJavaVM

        public static JavaVM findJavaVM​(int majorVersion)
        Loops from majorVersion up to 19 until a call to findJavaVMTryExact(int) returns a Java-Home.

        Returns the current JVM from getCurrentJavaVM(), if its major version is greater than or equal to the requested majorVersion.

        Parameters:
        majorVersion - the Java major-version to start with.
        Returns:
        a Java-VM on the local system or null, if no matching Java-Home could be found.
      • findJavaVMTryExact

        public static JavaVM findJavaVMTryExact​(int majorVersion)
        Find a Java-Home that exactly matches the given Java major version.

        Searches for the Java-Home in these places in this exact order:

        1. Environment variable JDKxx_HOME, where xx is the majorVersion.
        2. Environment variable JAVAxx_HOME, where xx is the majorVersion.
        3. System property jdkXX.home, where XX is the majorVersion.
        4. System property javaXX.home, where XX is the majorVersion.
        5. Using the /usr/libexec/java_home on MacOS, which may return a newer Java version.
        Parameters:
        majorVersion - the Java major-version to search for.
        Returns:
        a Java-VM on the local system or null, if no matching Java-Home could be found.
      • getCurrentJavaVM

        public static JavaVM getCurrentJavaVM()
        Get the JavaVM instance for the current JVM.
        Returns:
        JavaVM instance for the current JVM, never null.
      • currentJavaVMMajorVersion

        public static int currentJavaVMMajorVersion()
      • majorVersionFromString

        public static int majorVersionFromString​(String versionString)
        Extracts the major version from a Java-version-string as returned from System.getProeprty("java.version).
        Parameters:
        versionString - the Java-version-string
        Returns:
        extracted Java major version
      • forJavaHome

        public static JavaVM forJavaHome​(String javaHome)
      • forJavaHome

        public static JavaVM forJavaHome​(Path javaHome)
      • getJavaHome

        public Path getJavaHome()
      • getJavaExecutable

        public Path getJavaExecutable()