views:

66

answers:

4

Hi all, I want to compile jdk itself. I mean, I want to modify String.class in jdk to see created string objects in the system. Is there any way to modify classes in the jdk? When I try to modify source by modifying rt.jar, I got the error.

java.lang.NullPointerException at java.util.Hashtable.put(Hashtable.java:394) at java.lang.System.initProperties(Native Method) at java.lang.System.initializeSystemClass(Unknown Source)

Probably there is a signature problem.

Thanks,

+1  A: 

That doesn't look like a signature problem. It looks like you changed something that's causing Hashtable to dereference a null pointer. Review the change you made and see why it's doing this. Recall that Java keeps internal references to String constants in some conditions. I'm guessing you broke one of those.

Erick Robertson
A: 

Is there any way to modify classes in the jdk?

Well, you can download, modify and build the OpenJDK releases of Java 6 from source. There's information on how to do this on the OpenJDK site.

But beware that changes to low-level Java classes (such as String) can have effects that are hard to a non-expert to understand. And the consequence could well be a JVM that fails in a way that makes println or printStackTrace() inoperative.

Stephen C
A: 

There is maybe another way: you download java.lang.String original source, doing your modifications and compile only this class.

When you start your main programm be aware when loading classes: first load your String class then the java runtime classes. Refer to the java manual and the -Xbootclasspath/p options to do it in the proper order.

PeterMmm
A: 

When installing a JDK you may choose to install the sources as well. Do so. Or download the sources separately. Then

  • expand the src.zip and get the String.java file.
  • create a new project containing the String.java in package java/lang.
  • change it accordingly to your needs.
  • just compile it.
  • put the class into the bootclass path of your JDK. See More infos on Bootclassbath.
  • run your app.

java -Xbootclasspath/p:<changed String classpath> -cp <regular classpath> <your application main class>

But changing the JDK might not be a good idea and you are not allowed to ship a changed JDK (at least up to 1.6) due license restrictions.

And yes, your problem is most likely somewhere else. Remember select isn't broken ;-)

Peter Kofler
Even Sun/Oracle 1.6 has restrictions. IIRC, IANAL: Under JRL you can share changes for research purposes. JIUL allows changes to be used in *internal* production systems. OpenJKD 6/6-open (a backport of an early OpenJDK 7) is GLP with CLASSPATH exception.
Tom Hawtin - tackline
You are right, I meant up to including. I suppose OpenJKD is free then, but I don't know...
Peter Kofler