How does Java link lib / rt.jar to your application at runtime?

Java standard / system libraries ( java.*

, javax.*

etc.) are stored lib/rt.jar

inside each JRE distribution.

Let's say I have an application that I have compiled and jar

red in myapp.jar

. This JAR contains only my application class files, and simply refers to the system classes, such as System

, File

, Runtime

, Thread

, String

, Boolean

, etc.

So when I run my application, say through java -jar myapp.jar

, the JVM obviously makes the last link (or something) when it executes the bytecode of my class files (inside myapp.jar

) and then "jumps" in lib/rt.jar

to run the byte -code located there. I would assume the process is the same if it myapp.jar

depends on other JARs provided at runtime classpath

.

My question is, what is the name of this "glue" process and how does it work?

+3


source to share


1 answer


Something that rt.jar

is part of the bootstrap classpath, the parent of the regular classpath you already know and which you customize when using the parameter -cp

(you can also change the bootstrap classpath using the bootstrap option -Xbootclasspath

, such as a custom Java runtime).

See the Oracle documentation for a detailed description of how classes are searched for / loaded from a system-defined class hierarchy.



Now you have additional questions:

  • How was the archive actually found?

    It's just hardcoded. If the binary java

    is in <common_root>/bin/java

    , the search for rt.jar will be done in <common_root>/lib/rt.jar

    .

  • How is "binding" done?

    There is no real binding in the JVM, the classes are dynamically loaded using a mechanism based on the ClassLoader hierarchy, which are software components that actually do the loading / parsing of the class file. When you try to load a class, the search starts with the default loadable classloader (or child classloader if you defined one), and if the class cannot be loaded, the load attempt is repeated with the parent classloader until the loadloader bootstrap will not load reached.

    If the class is found, the file .class

    , parse, and internal structures representing the class are loaded and its data is generated. When the class is loaded, a new instance can be created. If instead even the classloader was unable to load your class, then the user-visible is displayed ClassNotFoundException

    .

+1


source







All Articles