Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the jvm load my java.lang.String instead of the one in rt.jar

I studied java classloader rencently. Now I want to write a class that has the same package name and class name as one of class in rt.jar. For example, write a java.lang.String class by myself, and how to break the parents delegation model to make the jvm load my java.lang.String instead of the one in rt.jar.

Re-edit
Thx, tried. And ↓↓↓


    D:\>java -verbose -Xbootclasspath/p:D:/myrt.jar -jar exe.jar
    [Opened D:\myrt.jar]
    [Opened C:\java\jre\lib\rt.jar]
    [Loaded java.lang.Object from C:\java\jre\lib\rt.jar]
    [Loaded java.lang.String from D:\myrt.jar]
    [Loaded java.io.Serializable from C:\java\jre\lib\rt.jar]
    [Loaded java.lang.reflect.GenericDeclaration from C:\java\jre\lib\rt.jar]
    [Loaded java.lang.reflect.Type from C:\java\jre\lib\rt.jar]
    [Loaded java.lang.reflect.AnnotatedElement from C:\java\jre\lib\rt.jar]
    [Loaded java.lang.Class from C:\java\jre\lib\rt.jar]
    Invalid layout of java.lang.String at value
    #
    # A fatal error has been detected by the Java Runtime Environment:
    #
    #  Internal Error (javaClasses.cpp:136), pid=6968, tid=4116
    #  fatal error: Invalid layout of preloaded class
    #
    # JRE version:  (7.0_45-b18) (build )
    # Java VM: Java HotSpot(TM) 64-Bit Server VM (24.45-b08 mixed mode windows-amd64
     compressed oops)
    # Failed to write core dump. Minidumps are not enabled by default on client vers
    ions of Windows
    #
    # An error report file with more information is saved as:
    # D:\\hs_err_pid6968.log
    #
    # If you would like to submit a bug report, please visit:

like image 307
young.jd.yang Avatar asked Feb 27 '14 06:02

young.jd.yang


People also ask

What is RT jar file in JVM?

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.

Is RT jar optional?

rt. jar stands for runtime and contains all of the compiled class files for the core Java Runtime environment. 2) You must include rt. jar in your classpath, otherwise you don't have access to core classes e.g. java.

Can JVM can exist without a class loader?

Each application might use different versions of the same libraries, and must thus have a different classloader from the others in order to be able to have different versions of the same classes in a single JVM. but the web server has its own loader.it can have several classloaders.


1 Answers

You can do this using the -Xbootclasspath/p option at JVM startup:

-Xbootclasspath/p:/path/to/yourimpl.jar

/p stands for "prepend".

Note: -Xbootclasspath isn't a standard java option, so JVMs by different vendors may not support it.

like image 114
fge Avatar answered Nov 10 '22 01:11

fge