Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is rt.jar not part of the class path system property?

I use the system property java.class.path to find all jars and directories that belong to the classpath. At first sigth this seems to work great. But on closer inspection I found that when I execute my program with gradle there is no rt.jar in this classpath. When executed from IntelliJ IDEA ´rt.jar´ is part of that class.

Why do the classpaths between IntelliJ and Gradle differ in this way?

Just to be clear: All the classes contained in rt.jar get loaded just fine, it is just the property that is confusing me.

like image 911
Jens Schauder Avatar asked Oct 24 '25 14:10

Jens Schauder


2 Answers

rt.jar doesn't need to be in the classpath, since it is already in the bootclasspath. It is safe to remove it from your classpath.

You can refer to this Oracle doc: http://docs.oracle.com/javase/8/docs/technotes/tools/findingclasses.html

How the Java Launcher Finds Bootstrap Classes

Bootstrap classes are the classes that implement the Java 2 Platform. Bootstrap classes are in the rt.jar and several other jar files in the jre/lib directory. These archives are specified by the value of the bootstrap class path which is stored in the sun.boot.class.path system property. This system property is for reference only, and should not be directly modified.

It is very unlikely that you will need to redefine the bootstrap class path. The nonstandard option, -Xbootclasspath, allows you to do so in those rare cicrcumstances in which it is necessary to use a different set of core classes.

like image 88
Alexandre Santos Avatar answered Oct 27 '25 08:10

Alexandre Santos


By default, there are three ClassLoader's in the JVM:

  1. Boot class loader - including rt.jar
  2. Extension class loader - including JAVA_HOME/jre/lib/ext
  3. Application class loader - java.class.path

You can see rt.jar on the boot class path by printing java.lang.ManagementFactory.getRuntimeMXBean().getBootClassPath(). I'm not familiar with IDEA, so I have no idea why IDEA includes rt.jar in java.class.path, but I suspect it is redundant since those classes will all be loaded by the boot class loader anyway.

like image 27
Brett Kail Avatar answered Oct 27 '25 09:10

Brett Kail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!