Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reliably locate Java's rt.jar or equivalent?

How can one reliably locate the jar containing Java's bootstrap classes (rt.jar or equivalent)? I was using the below code, but I've discovered that JAVA_HOME is used to refer to the JDK, not the JRE, and will fail entirely if there is no JDK installed.

def findJRE():
    try:
        home = os.environ['JAVA_HOME']
        path = os.path.join(home, 'jre', 'lib', 'rt.jar')
        if os.path.isfile(path):
            return path

        #For macs
        path = os.path.join(home, 'bundle', 'Classes', 'classes.jar')
        if os.path.isfile(path):
            return path
    except Exception as e:
        pass
like image 533
Antimony Avatar asked Oct 20 '12 06:10

Antimony


People also ask

Where can I find RT jar?

3) In windows, rt. jar will always reside under $JAVA_HOME/jre/lib, where $JAVA_HOME refers to the JDK installation directory. Even if you don't install JDK and just install JRE, you will see it in the exactly the same location, you won't find rt. jar inside $JAVA_HOME/lib directory.

What is JAR file Where is Rt jar present & what it contains?

rt. jar contains all of the compiled class files for the base Java Runtime environment, as well as the bootstrap classes, which are the run time classes that comprise the Java platform core API.

Where is Rt jar in Java 9?

jar and tools. jar in JDK 9. Class and resource files previously stored in lib/rt. jar , lib/tools.

What replaced RT jar?

2 Answers. Show activity on this post. The rt. jar file was removed in Java 9, but if you need to access the classfiles in the runtime, you can do so easily through the JRT file system.


Video Answer


2 Answers

As the location and name of the file differs between platforms and it seems there is no environment variable pointing to it I suppose that your best bet is to look for the file in the filesystem:

#!/usr/bin/env python
import subprocess
import re

rtjarPaths = subprocess.check_output(["locate", "rt.jar"])
paths = re.findall('^.*/jre/.*$', rtjarPaths, re.M)
print paths

vicent@deckard:~$ python findrt.py 
['/usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar']

The above code works fine for me on my Ubuntu box. It can be easily extended for working on MacOS X too. Other linux distros may require to adapt the code too.

Update: After some googling I've found in the oracle documentation that the rt.jar file is located in the path stored in the sun.boot.class.path system property. Unfortunately I don't know how to get this property directly from Python or from the command line so I can only provide the following dirty alternative to the previous code.

Create and compile PropertiesTest.java:

public class PropertiesTest {
    public static void main(String[] args)
        throws Exception {
        String value = System.getProperty("sun.boot.class.path");
        System.out.println(value);
    }
}

Then execute the following Python script:

#!/usr/bin/env python
import subprocess
import re

jrePaths = subprocess.check_output(["java", "PropertiesTest"])
rt = re.findall('(?:.*:)?(.*/(?:rt|classes)\.jar):?', jrePaths)
print rt

which should work on both Linux and MacOS X platforms. On my Ubuntu system it gives the output

['/usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar']
like image 162
Vicent Avatar answered Sep 30 '22 00:09

Vicent


I like Vincent's more principled answer, but here is an even simpler hack:

java -verbose 2>/dev/null | sed -ne '1 s/\[Opened \(.*\)\]/\1/p'

prints the absolute path to rt.jar.

Tested with Sun Java 6 and 8 on Linux and Windows (Cygwin), and Sun Java 6 on OS X (where rt.jar is instead called classes.jar).

like image 23
ntc2 Avatar answered Sep 30 '22 01:09

ntc2