saker.build Documentation TaskDoc JavaDoc Packages
public class JavaTools
Utility class for handling JRE and JDK resources.
Methods
public static Path
Gets the path to the java executable of the current JRE installation directory.
public static int
Gets the current Java Runtime major version.
public static String
Gets the current value of the java.version system property.
public static ClassLoader
Gets a classloader that can load the JDK related compiler and documentational classes.
public static ClassLoader
Gets the JDK tools classloader if it is already loaded.
public static ClassLoaderResolver
Gets a classloader resolver instance that resolves the classes in the JDK tools classloader.
public static Class<?>
Gets the main class for the jarsigner tool.
public static Path
Gets the path to the java executable for a specified JRE or JDK installation directory.
public static Path
Gets the path of the installation directory of the current JRE.
public static DocumentationTool
Gets a reference to a DocumentationTool instance.
public static JavaCompiler
Gets a reference to a JavaCompiler instance.
public static Path getCurrentJavaExePath()
Gets the path to the java executable of the current JRE installation directory.

The returned path has the same semantics as getJavaExeProcessPath(Path).

The java executable path.
public static int getCurrentJavaMajorVersion()
Gets the current Java Runtime major version.

This major version of the JRE is incremented for each major release. The first release that this method supports is release 8, so this method will always return a value greater than that.

This method determines the JRE version by checking for the exitence of th java.lang.Runtime.Version class and if it exists, the major() method will be called on the instance retrieved from Runtime.Version().

If the version class is not found, it means that the current JRE doesn't contain that class, which can only happen if we're running on older releases than JRE 9. As this class will always be compiled for 8 or later releases, the major version 8 will be returned if the version class is not found.

The runtime major version.
Gets the current value of the java.version system property.

The returned value is cached during the initialization of this class, and will return the same values, even if the system property is changed meanwhile.

The value of java.version system property.
Gets a classloader that can load the JDK related compiler and documentational classes.

The returned classloader can be used as a parent classloader to access classes for the Java Compiler API. (E.g. the source Trees).

On JDK8, this method will try to load the lib/tools.jar from the installation directory. The classloader will be cached using weak or soft references.

On later versions (JDK9+), this method will return the platform classloader.

It is recommended to call this instead of ToolProvider.getSystemToolClassLoader(), as that is deprecated in JDK9.

The classloader for accessing JDK classes.
IOExceptionIn case of I/O error. (If the tools.jar is not found or failed to load.)
Gets the JDK tools classloader if it is already loaded.

This method is the same as getJDKToolsClassLoader(), but returns null if the ClassLoader has not yet been loaded.

The classloader for accessing JDK classes or null if not yet loaded.
saker.build 0.8.12
Gets a classloader resolver instance that resolves the classes in the JDK tools classloader.

The returned instance is a singleton, it will not be garbage collected when no longer referenced. I.e. it can be registered to a ClassLoaderResolverRegistry without keeping a strong reference to the returned resolver.

The classloader resolver.
public static Class<?> getJarSignerMainClass() throws RuntimeException
Gets the main class for the jarsigner tool.

The method retrieves the class that contains the main(String[]) method for the jarsigner utility.

For JDK8, the method will retrieve the sun.security.tools.jarsigner.Main from the tools.jar of the JDK.

For JDK9 or later, the method will get the sun.security.tools.jarsigner.Main class from the platform classloader.

In general, it is recommended that callers use the JarSigner class to perform JAR signing operations on JDK 9 or later.

The main class for the jarsigner utility.
RuntimeExceptionIf the class was not found. Callers should gracefully handle this scenario.
saker.build 0.8.10
public static Path getJavaExeProcessPath(Path jreorjdkdir)
Gets the path to the java executable for a specified JRE or JDK installation directory.

The method will resolve the java path in the bin directory of the specified argument. If the argument has already a file name of bin then it won't be resolved twice.

The method resolves java as the last path, and will not behave distinctly for different operating systems. I.e. it will not resolve java.exe for Windows. Based on our experience, ProcessBuilder will accept the returned path without the .exe extension when starting processes, which is most likely what the caller wants.

jreorjdkdirThe JDK or JRE installation directory.
The path to the java executable.
Gets the path of the installation directory of the current JRE.

The returned value points to the root directory of the installation.

If the installation is JDK8, then the result will point to the root installation directory, not the subdirectory for the accompanying JRE. (I.e. the result is the install dir, not its jre subdirectory.)

It can be expected that there is an excecutable under this directory with the path bin/java or bin/java.exe.

The installation directory.
Gets a reference to a DocumentationTool instance.

It is recommended to call this instead of ToolProvider.getSystemDocumentationTool(), as the implementation can cache the classloader in relation with the getJDKToolsClassLoader() call.

A documentation tool instance.
Gets a reference to a JavaCompiler instance.

It is recommended to call this instead of ToolProvider.getSystemJavaCompiler(), as the implementation can cache the classloader in relation with the getJDKToolsClassLoader() call.

A Java compiler instance.