Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 10 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

General: Recompile (Some of) The JDK to Ease Debugging

At 2:38 PM on Jul 28, 2005, R.J. Lorimer wrote:

Recently my team at work has been working with the javax.swing.text package a lot (as some of you may know from hearing me complain in my tips previously), and it has been really running me through the ringer on certain occasions. Unfortunately, the techniques for extending classes such as the DefaultStyledDocument become increasingly blurry as the documentation becomes less clear, and the source code becomes more complicated. It's not really a fault of the original styled document authors, it is just a very complicated and abstract problem (managing line breaks and text element positioning), and the implementation is quite complex because of that.

Partly because of this complexity, and partly because of the unusual ways we are using the styled document on my current project, we have had a lot of unusual bugs and problems show up in our application as we are testing in preparation for packaging it up for deploy. This typically requires some form of debugging.

Unfortunately, Swing (as well as the rest of the runtime libraries that ship with Java) is compiled without debug options enabled. I can only assume that this is to prevent the JDK download to balloon to a huge size. There are bugs entered into the Java Bug Database about this specific problem (http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4652184) along with instructions that are supposed to make it possible to do a complete re-compile with debug options on (although I haven't gotten them to work; it's quite an undertaking).

What this really means for a debugger is that you can step into a given method, and can follow the execution, but you won't have any variable information. In some cases this may be sufficient, but in most (particularly when you are dealing with sufficiently complex code such as javax.swing.text.DefaultStyledDocument.ElementBuffer), it becomes nearly useless to even try to debug. Unfortunately, a debugger is a very useful tool to understand what the code is actually doing, and it would be desirable to be able to trace the execution to see where things go wrong.

A co-worker of mine, Adi Rosenblum , was also lamenting the difficulties with not having variable information, and was able to find a workaround that covered our case (and an overwhelming portion of cases I would guess) by repackaging the existing rt.jar with new results from the src.zip file that ships with every JDK. He ended up writing a quick script so that the rest of the team could compile what they needed, and so it could be reused later.

Essentially here is what the script does:

  • Takes the source-code you want to recompile, and compiles it with the existing rt.jar as the classpath (with the debug options enabled, obviously).
  • Extracts the existing rt.jar into a folder.
  • Overlays the result of the newly compiled code on top of the exploded result of the rt.jar.
  • Re-jars that exploded result into a new rt.jar.

All the script needs from you is for you to filter out what you want to be compiled, and provide the directory structure, and then for you to move the rt.jar to your JRE installation folder when it is complete.

Please be aware, this script *is* a workaround, and as such, it can't compile some of the more obscure classes that may not be part of the public JDK libraries, and it doesn't do anything about native libraries - so, if you need a *full fledged* debug rt.jar, you might want to read about more here .

To get started then, simply go to the directory where your JDK is installed and get the source zip (should be JDK_HOME/src.zip ). Then, extract that into a temporary folder somewhere. This folder should only have a subset of the classes that are in src.zip - you can play with this set until they all compile correctly, but I have found that taking all of java/**/* , javax/**/* , and all of org/**/* satisfies all of my debugging needs, and compiles fine all the way up to Java 5. This covers XML, java.util, java.lang, java.io, java.nio, javax.swing, java.awt, etc. - so typically is good enough.

Here is a screenshot of my source folder prior to the compilation:

The script is optimized to make compilation as quick as possible, we've found it only takes a couple minutes. Adi discovered that if instead of just telling 'javac' to 'compile everything under the source folder', by giving it a file list produced off of all of the files with a .java extension (which is really the same thing) he was able to make javac literally orders of magnitude faster.

The next step is to set the script up for your platform. All of the variable information is isolated to the top of the script, and is documented quite well. It primarily consists of 1.) where your existing JRE is, 2.) where the source to recompile is, 3.) what level the source should be compiled at, and 4.) where the javac command to use is. Once you have it configured, you should be able to simply execute it. Here is what the top of my script looks like:

@echo off
:: You must set the following variables.
:: SRC_DIR - Location of .java files to be compiled into the new rt.jar
set SRC_DIR=src
:: DESTINATION_DIR - Final location to copy new rt.jar into
set DESTINATION_DIR=.
:: RT_JAR - Full path to the location of an existing rt.jar to use as the source (Will be left unharmed).
set RT_JAR="C:\Program Files\Java\j2re1.4.2_08\lib\rt.jar"
set LEVEL=1.4
set JAVA_BIN=C:\Program Files\Java\j2sdk1.4.2_08\bin\

:: No need to modify anything below this comment.

Once it is done, simply go to the lib directory of the JRE you are debugging with (typically either [JRE_HOME]/lib or [JDK_HOME]/jre/lib), and rename the rt.jar there so you can back it up (say, rt_old.jar). Then, move your new rt.jar in. That's it!

I have attached the script in full so you can peruse it for yourself. I apologize for all you non-Windows users, it is a Windows batch script. The good news is if you can do it with a Windows batch script, it surely can be done with a script on any other platform, and probably better. The script is fairly simple in nature anyway, and is well documented, so hopefully you won't have any problems.

Until next time,

R.J. Lorimer
Contributing Editor - rj -at- javalobby.org
Author              - http://www.coffee-bytes.com
Software Consultant - http://www.crosslogic.com

1 . At 2:53 PM on Jul 28, 2005, Santhosh Kumar T DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: General: Recompile (Some of) The JDK to Ease Debugging

you don't need to touch rt.jar at all. Just create a jar with new classs and drop it in $jdk/jre/lib/ext/endorsed.

or use -Djava.endorsed.dirs runtime argument when you want to use your jar.

for more details see:
Endorsed Standards Override Mechanism
Santhosh Kumar T - santhosh@fiorano.com
MySwing Project - https://myswing.dev.java.net/
Cool Swing Stuff - https://myswing.dev.java.net/MyBlog/MySwingTree.html
Sr. Software Eng - http://www.fiorano.com
2 . At 2:55 PM on Jul 28, 2005, R.J. Lorimer wrote:
  Click to reply to this thread Reply

Re: General: Recompile (Some of) The JDK to Ease Debugging

Santosh,

That is a good point. Thanks.
Best, R.J. Lorimer
3 . At 8:39 PM on Jul 28, 2005, Will Hartung DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: General: Recompile (Some of) The JDK to Ease Debugging

Just to stoke the fires and all, but, umm...why didn't you do this as an Ant build file? Ye Olde Portable Lingua Franca for many Java projects.

Just curious.
4 . At 7:27 AM on Jul 29, 2005, Harrus wrote:
  Click to reply to this thread Reply

Re: General: Recompile (Some of) The JDK to Ease Debugging

I had a similar problem with jhelp. I added the source files to my project and removed the checksum in MANIFEST.MF of jh.jar because it caused a version error. Local files are favored over files in jar files.
I even ship some altered jhelp classes in my own jar. These classes have package level access and it superimposes the files in jh.jar. It was not possible to derive from these classes, because my changes concerned only a few internal classes.
5 . At 8:11 AM on Jul 29, 2005, Maris Orbidans wrote:
  Click to reply to this thread Reply

Re: General: Recompile (Some of) The JDK to Ease Debugging

> I even ship some altered jhelp classes in my own jar.

Is it legal to do such things ?
6 . At 12:00 PM on Jul 29, 2005, Harrus wrote:
  Click to reply to this thread Reply

Re: General: Recompile (Some of) The JDK to Ease Debugging

Well, I didn't publish it yet, but in this case it's not a problem since it's published on java.net and the license changed to SUN PUBLIC LICENSE.

2.1 The Initial Developer Grant.

The Initial Developer hereby grants You a world-wide, royalty-free,
non-exclusive license, subject to third party intellectual property
claims:

(a) under intellectual property rights (other than patent or
trademark) Licensable by Initial Developer to use, reproduce, modify,
display, perform, sublicense and distribute the Original Code (or
portions thereof) with or without Modifications, and/or as part of a
Larger Work; and ...
7 . At 2:34 PM on Jul 29, 2005, Adi Rosenblum wrote:
  Click to reply to this thread Reply

Re: General: Recompile (Some of) The JDK to Ease Debugging

Using Ant would be easy enough, but I'm just not an expert in it. I had some javac issues initially, so using a batch file for me was simply faster to resolve those (yes, I'm embarrased to say that). I'll probably write an Ant script now that I proved the new rt.jar worked for me. If you wrote one, please post...
8 . At 9:42 AM on Aug 1, 2005, Richard Osbaldeston wrote:
  Click to reply to this thread Reply

Do you mean something like this?

http://www.javalobby.org/forums/thread.jspa?messageID=91825915#91825965
9 . At 10:00 AM on Aug 1, 2005, R.J. Lorimer wrote:
  Click to reply to this thread Reply

Re: Do you mean something like this?

Richard,

Thanks for posting this! I remembered that discussion and went looking for it, but couldn't find it for some reason or another. I didn't see that there was discussion of the actual process supplied in the tip however, that's great! I will try to update the actual tip this afternoon to reference that discussion and the Ant build file.

Regards,
Best, R.J. Lorimer
10 . At 1:17 PM on Aug 1, 2005, Dmitri Trembovetski DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: General: Recompile (Some of) The JDK to Ease Debugging

You can also just put your modified classes on the boot class path with -Xbootclasspath/a: , no need to recreated rt.jar or copy anything.

Thanks,
Dmitri
Java2D Team
Java2D Team Sun Microsystems, Inc

thread.rss_message