Reputation: 59
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?
Upvotes: 1
Views: 562
Reputation: 20803
NoClassDefFoundError
most of the time is accompanied or caused by a ClassNotFoundException
so you should really look for that.
So in your stack trace you have :
Caused by: java.lang.ClassNotFoundException: java.lang.StringBuilder not found in [file:/usr/share/java/libgcj-3.4.5.jar, file:./, core:/]
That is the root cause of your exception.
You actually should never face this issue if you were to use Oracle JDK/JVM instead of GNU compiler(gcj
) for Java ( Which seems to be default java in a lot of Linux's). Download Oracle JDK/JVM, install it and set that as your java
command (JAVA_HOME
and in PATH
environment variable) and this error would go away.
Upvotes: 1
Reputation: 522712
The root cause of your error is that the JVM cannot find the java.lang.StringBuilder
class. But this is ultimately a result of your using an outdated libgcj
JDK instead of using a standard Sun-compliant JDK.
From the libgcj
documentation:
Just look in the 'libjava' directory of libgcj and see what classes are there. Most GUI stuff isn't there yet, that's true, but many of the other classes are easy to add if they don't yet exist.
Apparently StringBuilder
isn't in there, which is why you are having the problem. Make your life easy and just download Java 7 or Java 8 from the Oracle website. As @JonSkeet mentioned (and is correct as usual), you are using Java 4, which is seriously outdated.
Upvotes: 2
Reputation: 1503459
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.)
Upvotes: 5