Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Dodge the traps hiding in the URLConnection class

The URLConnection class's generic design causes snags when posting to a URL

  • Print
  • Feedback

Page 5 of 6

Pitfall 6: JVM throws NoClassDefFoundError

Pitfall 6 -- in which the JVM throws a NoClassDefFoundError -- is guaranteed to surface numerous times in every introductory Java course. The JVM's inability to locate the classfiles it needs crops up even after the programmer has learned to properly set the classpath. I recently encountered this problem while trying to teach a programmer to run a Java program from the command line instead of from within Symantec's integrated development environment (IDE). Her classpath and path were set fine; however, the JVM continued to throw the NoClassDefFoundError. To solve this problem, you need to know what the VM thinks the classpath is, not what you think it is.

To find out what the beginner programmer's VM thought the classpath was, I wrote a simple command line utility to print out the classpath:

Listing 6.1 PrintClassPath

import java.util.*;
public class PrintClassPath
{
    public static void main(String args[])
    {
        try
        {
            System.out.println(System.getProperty("java.class.path"));
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
}


On my Windows NT box, a run of PrintClassPath produced:

E:\classes\com\javaworld\jpitfalls\article2>java PrintClassPath 
e:\java\rhino\js.jar;e:\borland\interclient\interclient.jar;e:\Program 
Files\InterBase 
Corp\InterClient\interclient.jar;e:\javapps\jcannery;e:\java\xml-soap\lib\s
oap.jar;e:\classes;.;e:\jdk1.1.8\lib\classes.zip;e:\java\xerces-1_2_0\xerce
s.jar;e:\java\jaxp1.0\jaxp.jar;e:\java\jaxp1.0\parser.jar;e:\java\JClass36lib\jctable362.jar;e:\java\JClass36;e:\java\JClass36;e:\java\JClass36\lib\j
cbwt362.jar;e
:\java\JClass36;e:\synergysolutions\Xml-in-Java;e:\Program 
Files\Netscape\Progra
m\Java\Classes\java40.jar;e:\java\jdom\build\jdom-b4.jar


With this program, it quickly became evident that the virtual machine was not using the classpath set in the environment variable. The problem became clear when the programmer explained that she was using the built-in VM that came with her Symantec Visual Café IDE. That VM uses an sc.ini text file instead of the environment variable in DOS. After changing the classpath in the sc.ini file, everything worked fine.

Nine times out of ten, when you see NoClassDefFoundError, it is a classpath issue. With the simple command line tool in Listing 6.1, you can quickly ascertain what classpath the virtual machine thinks it is using. Most of the time you will find that the classpath includes a typographic error, or the directory structure does not match the package name.

Developing for both Java 1.2 and Java 1.1.8 can be quite difficult. You must be careful to not mix your environments, and especially careful not to use classes that are incompatible with your target JVM. To test an applet, I recently switched my VM from 1.2 to 1.1.8 without switching my classpath, since it correctly pointed to the target classes. Unfortunately, I then received the dreaded NoClassDefFoundError on a class that was clearly present. Listings 6.2 and 6.3 demonstrate the problem.

  • Print
  • Feedback

Resources