According to Wikipedia, there are 3 class loaders.

The Boot Class-Loader which loads everything in RT.jar and /../jre/lib

Then the extensions class loader and the system class loader.

I'm only interested in the Boot class loader because I want to hook it and redefine a class in RT.jar without all the legality issues that come with -Xbootclasspath:/p.

Docs: http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html

Note: Applications that use this option to override a class in rt.jar should not be deployed. Doing so would contravene the Java 2 Runtime Environment binary code license.

And the license here: http://www.oracle.com/technetwork/java/javase/downloads/jre-6u21-license-159054.txt


C. Java Technology Restrictions. You may not create, modify, or change the behavior of, or authorize your licensees to create, modify, or change the behavior of, classes, interfaces, or subpackages that are in any way identified as "java", "javax", "sun" or similar convention as specified by Oracle in any naming convention designation.

So I'm thinking that if I can hook the boot class loader from native code via C++, I can replace a class in by hooking the JVM_DefineClass function from JVM.dll without any legal trouble.

The function has the signature:

JNIEXPORT jclass JNICALL JVM_DefineClass(JNIEnv *env, const char *name, jobject _loader, const jbyte *buf, jsize len, jobject pd)

  • 1: Does this function actually get call when a JVM is starting up?
  • 2: Would I still be breaking the license if I hook it?
  • 3: Am I hooking the right function or should I be doing something else(better method maybe?)?
  • 1
    Not a lawyer, but using the boot class loader to replace a class would probably still qualify as changing the behaviour of that class. Besides, very probably you don't have the resources to defend yourself against oracle should they sue you. – Guntram Blohm Feb 18 '15 at 8:03
  • 4
    I'm voting to close this question as off-topic because it is asking for legal advice. – Antimony Feb 18 '15 at 8:38
  • Well.. I did it anyway. It turns out the JVM calls the above function (JVM_DefineClass from JNI.dll) to define a class. It also calls Java_lang_lang_DefineClassX from Java.dll where X is a number from 0 to 2 inclusive. Replacing the bytes with your own allows you to redefine any class. Of course, Xbootclasspath is easier though. – user11780 Feb 18 '15 at 17:00