Digital Edition

Plumbing Java Classes
Plumbing Java Classes

I've been working with Java for almost two and a half years now. I can say with confidence that I know pretty much what's going on in the Java core classes. Through the Java training I do, I try to pass an understanding of Java to my students (and hopefully through the students the same information can be passed on to other people who want to learn about Java). What I'd like to pass on in this month's Tips and Techniques column is the technique I've found has taught me more about how Java works than any other.

You'd think that the distribution of the source code for the Java core APIs(the classes in the java.* packages) would be sufficient to tell you most anything you would want to know. For example, if you really want to know how the Abstract Windows Toolkit is structured and works, There's nothing that's going to tell you as much as analyzing the classes in the java.awt package yourself. Books and on-line documentation will tell you how to use the AWT, but only a good understanding of the construction of the AWT classes will allow you to have string confidence in your expertise.

Unfortunately, the full implementation of the AWT, and most of the other java.* packages, is not included in the source code release of the Java Development Kit (the JDK). For example, the details of the HTTP and FTP client classes that are distributed with the JDK are just not available. Another example: many Java interfaces that are well documented in the JDK do not have any examples for interface implementations. Take the java.applet.AppletContext and java.applet.AppletStub interfaces, for instance. No actual implementation of these Java interfaces is included with the JDK.

I'll admit something else here that I think will probably get me a little bit of hate e-mail. Sometimes I like to know how other Java developers do some of the really cool things I run across on the Web. Sometimes I run across an applet or a demo application, and I say to myself, "Wow! How did they do that?" And sometimes I employ the tools I'm about to describe to discover exactly how the developers did what I thought was so cool.

So for both of the situations, when I want to know more about the implementation of the core Java classes and when I just have to know how a particular trick was implemented, I employ Java decompiling tools to find out what I want to know. Maybe by admitting this fact publicly I'll be forgiven by the patron god of programmers. (Are you there, Bill? It's me, Brian.)

I'm not the first nor the only developer to consider employing decompilers to dive into Java classes. In fact, one of the decompiling tools I'm going to describe here, called a javap, was distributed with the Beta version of Java. Clearly, the Java designers thought this was a tool a significant number of Java engineers would want to have and would employ.

The javap tool is a program, distributed with the Java Development Kit, that can convert a compiled .CLASS file into a readable version of the Java class. The readable file is not a reproduction of Java source code. Instead, the javap tool is only capable of making the bytecode in the .CLASS file readable. I find this tool less and less preferred as time goes on, especially since much more capable tools have been developed. The reconstruction of the bytecode is sometimes necessary to answer questions about the Java language and VM.

For example, the first time I used javap (I remember it well) was when I couldn't find adequate Java documentation to answer this question: When exactly are in-line instance variable initializers performed?" That is, if I have this simple class definition:

class MyFOO {
public int m_MyFooNum =

public MyFoo {
if (0 ! = m_MyFooNum)
m_myFooNum = 100;

public static int MyFooStaticMethod(int i) {
//returns something other than 0

Then, what will the instance variable m_MyFooNum be equal to after the constructor is run? There are two options: If the in-line initialization is run before the code in the constructor, then the value of the variable will be 100 when the constructor is completed, no matter what the return value of MyFooStaticMethod is. On the other hand, if the constructor's code is run before the in-line initializer, then the variable will have the return value of MyFooStaticMethod. Granted, this example is a little contrived, but the questions, that of deterministic action of the Java compiler, can be quite crucial.

To figure the answer to this question, I might have gone to the Java language spec, but that's just not very exciting. Instead, I compiled a class that had this problem and then used the javap tool to decompile the resulting .CLASS file so that I could see that exact bytecode produced. As it turns out, the javap tool showed that the in-line initializer is always run before any code in a constructor. In fact, the in-line initializer is added as code at the beginning of every class constructor, including any default class constructor provided by the compiler. that picayune detail about the Java language is not important, but the use of the JDK's javap tool let me answer that question and turns out to have been very valuable for me.

Other Java questions I had in the future could not be answered by looking at any documentation because no documentation existed. Some of the sample questions:

  • How are RMI proxy and stub classes constructed, exactly?
  • What are Microsoft's Visual J++ COM/Java shim classes made of?

    The javap tool is included in the bin directory of the JDK distribution. If the JDK's java or javac tools are available through your system's search path, the the javap tool probably is, also. That is, if you can invoke the java interpreter of the javac compiler simply by typing "java" or "javac" on your system's command line, then you can probably just type javap to invoke the javap tool. I'm not going to go into the details of how you use this tool here so I can use the space to discuss other related tools and issues. Instead, I'll just encourage you to read the on-line documentation about the JDK tools available to the JavaSoft Web site (

    The decompilation of any of the Java core classes is a relatively simple task using the javap tool. These classes are distributed in the JDK in a file named CLASSES.ZIP. Using a zip file tool, such as WinZip (a shareware tool available at www. or PKZip, you can extract the .CLASS file for any of the Java core classes from the zip archive. Then, apply the javap tool to the .CLASS file to get a text version of the bytecode in the .CLASS file.

    For Java core classes that have the source code distributed in the JDK, this is a useless technique. The source code is available and should be used. Some classes in the CLASS.ZIP file are not strictly part of the Java core API and so do not have the source code available. For example, the URL protocol handler and content handler classes do not have associated source code available. When you need to know how these classes are implemented (or you just want to know) you can use the javap tool to at least take a look at the bytecode. From the javap tool output, it's just a matter of mental gymnastics to approximate the original class source code.

    Decompiling .CLASS files distributed on the Web is also a relatively simple affair using the javap tool. Let's say you see a really cool Applet with some great animation you would like to know the algorithm for. Just viewing the Applet embedded in an HTML doc doesn't readily allow you to do this. The trick is to generate the URL for the Applet's CLASS file yourself and use your browser to view that.CLASS file directly. Most browsers have a "Save the File" feature which you can use to then save the .CLASS file to your local disk. Not that you have a .CLASS file, you then apply the javap tool to decompile the downloaded .CLASS file and you've achieved your goal.

    For example, you see an HTML page with the URL This page has a cool applet in it. Step one is to view the source for the HTML file and find the <APPLET> tag for the embedded applet. The fields of the <APPLET> tag should tell you sufficient information to generate the URL for the Applet's .CLASS file. The .CLASS file's URL is a combination of the page's URL, the applet's CODEBASE (which may be an empty string or a URI) and the Applet class' name with the extension 'class'. Step two is to generate the applets' URL (e.g., Step three is to type this URL into the browser and view the file. What you'll see at this point is a lot of garbage characters, which is pretty useless by itself. Step four is to save the .CLASS file to a file on your local file system (e.g., to a file in your home directory called "TheApplet.class"). The final step is to apply the javap tool to the .CLASS file.

    Several people have also made .CLASS file to .JAVA source code file generators. My favorite is the WingDis decompiler available from WingSoft software ( for a nominal fee. Using such decompiler tools you can generate straight Java source code from a compiled .CLASS file. If you don't feel like slogging through readable bytecode, using such a tool is probably the best bet for you.

    Now that I've published how anyone in the world can grab a copy of your Applet and steal your code, I feel that I must comfort some of my readers with advice about how you can effectively prevent other users from decompiling your .CLASS files. I mention two techniques briefly.

    The first technique is very simple: Don't publish .CLASS files you don't want others to decompile on the Web. If you've implemented some really cool framework or communications scheme, protect it by not distributing it. Generally you're going to do this by running the class only on the server-side. Even if this means your Applet or push channel is going to run less efficiently, in many instances it's worth the extra hassle to protect your code.

    The second protective technique, if you can't restrict the distribution of your classes, is a very old technique called "obfuscation". This involves essentially renaming all the public, protected and private members of your classes, as well as the classes themselves, using strings of random characters. So, while I may be able to understand what's going on in your class, and you have a class named "CreditCardProccessor", and I might be tempted to steal and use your class because it has a public method named "PerformTransaction", I won't able to even understand your class with the name "ksdf98whlwf98wf" and the public method "ks9wkf8". It's a simple and crude technique, but it has been used effectively for years.

    A couple of code obfuscation tools have been created to generate obfuscated classes from your Java source code or compiled .CLASS files. Search using your favorite Web search engine using the string "Java and obfuscat*". You should get a few hits of interest.

    Using these two techniques for protecting your code, you should be able to effectively protect your Java classes from theft or casual hacking.

    About Brian Maso
    Brian Maso is President of Blumenfeld & Maso, Inc., a Java and Media consulting company. In addition to his involvement in several commercial Java projects, he is also the author of several Java books, including Osborne/McGraw-Hill's upcoming title "Visual J++ From the Ground Up". Brian is the Java guru of DevelopMentor's Java training courses. He has also written several Java white-papers for individual

  • In order to post a comment you need to be registered and logged in.

    Register | Sign-in

    Reader Feedback: Page 1 of 1

    Subscribe to the World's Most Powerful Newsletters


    DXWorldEXPO LLC announced today that Dez Blanchfield joined the faculty of CloudEXPO's "10-Year Anni...
    "We started a Master of Science in business analytics - that's the hot topic. We serve the business ...
    The standardization of container runtimes and images has sparked the creation of an almost overwhelm...
    As DevOps methodologies expand their reach across the enterprise, organizations face the daunting ch...
    Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As au...
    The best way to leverage your Cloud Expo presence as a sponsor and exhibitor is to plan your news an...
    DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held Novemb...
    With 10 simultaneous tracks, keynotes, general sessions and targeted breakout classes, @CloudEXPO an...
    DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, ...
    As you move to the cloud, your network should be efficient, secure, and easy to manage. An enterpris...
    DXWorldEXPO | CloudEXPO are the world's most influential, independent events where Cloud Computing w...
    @DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held Novem...
    There is a huge demand for responsive, real-time mobile and web experiences, but current architectur...
    DXWorldEXPO LLC announced today that "Miami Blockchain Event by FinTechEXPO" has announced that its ...
    Cloud Expo | DXWorld Expo have announced the conference tracks for Cloud Expo 2018. Cloud Expo will ...
    Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to ...
    "NetApp is known as a data management leader but we do a lot more than just data management on-prem ...
    We call it DevOps but much of the time there’s a lot more discussion about the needs and concerns of...
    The dynamic nature of the cloud means that change is a constant when it comes to modern cloud-based ...
    "Since we launched LinuxONE we learned a lot from our customers. More than anything what they respon...