Find the standard Java library

What is the best way to find a jar containing basic Java classes such as java.lang.Object ? I use JAVA_HOME , but it turns out that this only works when installing the JDK. How can I find the standard library location that will be used if java were launched from the command line?

Also, I need to do this using a Python script, so please don't respond to Java libraries.

+6
source share
4 answers

When you start java from the Oracle JVM, it sets the sun.boot.class.path property to point to rt.jar , which usually contains all the java.lang elements.

What you can do is run a simple Java program that extracts System.getProperty("sun.boot.class.path") and gets where this file should be. Read more about it here .

The above should work if your environment is correct ( JAVA_HOME installed, etc.). Otherwise, I'm afraid you have a problem with the chicken and the egg ,-)

+2
source

Since there are no obvious restrictions on the placement of inner classes, you should try to process all possible (or as much as possible) places.

First of all, you should definitely look for java in PATH. Unfortunately, it sometimes fails, for example, you can often find java.exe in C:\Windows\System32 . Fortunately, in the case of Windows, you can use the registry to get a list of installed JREs.

If you find several jars that may contain base classes, you can use zip or jar to check for java/lang/Object.class .

+1
source

If you are using linux, you can use the shell command: "type java" and parsing.

0
source

Python should have tools to find where the executable "java" or "java.exe" executes. Follow all the symbolic links to show the true location. My Python is rusty, but I think shutil.which and os.path.realpath will do the job.

The main classes will be in ../lib/rt.jar relative to the java executable.

0
source

All Articles