Coming across NoClassDefFoundError in java

I come across an error while running this in Java:

// Test.java
public class Test {
    public static void main(String [] args) throws IOException {
        String a = "123";
        a = a + "456";
        System.out.println(a);
    }
}

by typing in these commands:

javac Test.java
java Test

And it has reported:

Exception in thread "main" java.lang.NoClassDefFoundError: while resolving class: Test
   at java.lang.VMClassLoader.resolveClass(java.lang.Class) (/usr/lib64/libgcj.so.5.0.0)
   at java.lang.Class.initializeClass() (/usr/lib64/libgcj.so.5.0.0)
   at java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) (/usr/lib64/libgcj.so.5.0.0)
   at gnu.gcj.runtime.FirstThread.run() (/usr/lib64/libgcj.so.5.0.0)
   at _Jv_ThreadRun(java.lang.Thread) (/usr/lib64/libgcj.so.5.0.0)
   at _Jv_RunMain(java.lang.Class, byte const, int, byte const, boolean) (/usr/lib64/libgcj.so.5.0.0)
   at __gcj_personality_v0 (/home/users/haotianyi/tr/java.version=1.4.2)
   at __libc_start_main (/lib64/tls/libc-2.3.4.so)
   at _Jv_RegisterClasses (/home/users/haotianyi/tr/java.version=1.4.2)
Caused by: java.lang.ClassNotFoundException: java.lang.StringBuilder not found in [file:/usr/share/java/libgcj-3.4.5.jar, file:./, core:/]
   at java.net.URLClassLoader.findClass(java.lang.String) (/usr/lib64/libgcj.so.5.0.0)
   at gnu.gcj.runtime.VMClassLoader.findClass(java.lang.String) (/usr/lib64/libgcj.so.5.0.0)
   at java.lang.ClassLoader.loadClass(java.lang.String, boolean) (/usr/lib64/libgcj.so.5.0.0)
   at _Jv_FindClass(_Jv_Utf8Const, java.lang.ClassLoader) (/usr/lib64/libgcj.so.5.0.0)
   at java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader) (/usr/lib64/libgcj.so.5.0.0)
   at _Jv_BytecodeVerifier.verify_instructions_0() (/usr/lib64/libgcj.so.5.0.0)
   at _Jv_VerifyMethod(_Jv_InterpMethod) (/usr/lib64/libgcj.so.5.0.0)
   at _Jv_PrepareClass(java.lang.Class) (/usr/lib64/libgcj.so.5.0.0)
   at _Jv_WaitForState(java.lang.Class, int) (/usr/lib64/libgcj.so.5.0.0)
   at java.lang.VMClassLoader.linkClass0(java.lang.Class) (/usr/lib64/libgcj.so.5.0.0)
   at java.lang.VMClassLoader.resolveClass(java.lang.Class) (/usr/lib64/libgcj.so.5.0.0)
   ...8 more

But the program runs OK when I remove the line a = a + "456";, i.e.,

// Test.java
public class Test {
    public static void main(String [] args) throws IOException {
        String a = "123";
        // a = a + "456";
        System.out.println(a);
    }
}

I wonder where is the problem and how to solve it?

Jon Skeet
people
quotationmark

The problem is that the compiler you're using is from Java 5 or later, so it's using StringBuilder instead of StringBuffer - but the execution environment you're using appears to be Java 1.4.2, which doesn't include StringBuilder.

Note that Java 1.4.2 is also ancient. I suggest you upgrade both the JDK and the JRE to a recent version (ideally straight to Java 8) - that will solve your issue and mean that you're not running on an unsupported version which may well have security bugs that haven't been patched because it's too old. (Along those lines, do you really need to use GCJ? I would stick to one of the more widely-used Java environments if you can.)

people

See more on this question at Stackoverflow