4

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.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348

3 Answers3

4

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.

Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64
3

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.

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
0

File -> Project Structure -> SDKs (IDEA on Mac)

You will see that IDEA had helped you load the classes what you need.

Randy
  • 36
  • 3