So...I have just begun to whet my appetite for Java today, and I have already run into a stumbling block. When I put my code in a .java file and compile it (via javac), I receive no errors. However, when I actually run my script, I receive quite the nasty error which I seem to not be able to comprehend. The error is as follows:

Exception in thread "main" java.lang.NoClassDefFoundError: test2.class
at gnu.java.lang.MainThread.run(libgcj.so.90)
Caused by: java.lang.ClassNotFoundException: test2.class not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.90)
at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.90)
at java.lang.ClassLoader.loadClass(libgcj.so.90)
at java.lang.ClassLoader.loadClass(libgcj.so.90)
at gnu.java.lang.MainThread.run(libgcj.so.90)

Probably the weirdest thing about this error is the fact that I only receive it when I run it via the terminal. If I run it via my IDE (Eclipse), I get no error! What's the deal here?

By the way, he is my code:

public class test2 {
	public static void main(String[] args) {
		Hello h = new Hello();
		h.hi();
	}

}

class Hello {
	void hi() {
		System.out.println("Hello");
	}
}

Thank you very much in advance.

Recommended Answers

All 8 Replies

Exception in thread "main" java.lang.NoClassDefFoundError: test2.class
at gnu.java.lang.MainThread.run(libgcj.so.90)
Caused by: java.lang.ClassNotFoundException: test2.class not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.90)
at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.90)
at java.lang.ClassLoader.loadClass(libgcj.so.90)
at java.lang.ClassLoader.loadClass(libgcj.so.90)
at gnu.java.lang.MainThread.run(libgcj.so.90)

I believe its simply a matter of configuration, its not finding the .class (which gets created when you compile the .java) it's most probably looking in the wrong directory.
Though, I don't personally know how to fix this error. but at least now you know why its happening =)!

So...I have just begun to whet my appetite for Java today, and I have already run into a stumbling block.

If you have just begun Java, then I would strongly recommend using the JDK (Java Development Kit which includes the javac compiler and other stuff) directly available from Sun itself here, rather than going for third party compilers like the GCJ.

Also after you compiled your application, do you see a 'test.class' file generated inside your current working directory ?

Okay, thanks for the suggestions. First of all, I actually am using the javac compiler. But let me just show you some terminal output that might help.

pat:~/Desktop/java$ls
test2.java
pat:~/Desktop/java$javac test2.java
pat:~/Desktop/java$ls
Hello.class test2.class test2.java
pat:~/Desktop/java$java test2.class
<error>

Thanks again.

When you compile you need to use file extension (*.java), however when you run it that is not case. Just use java test2

First of all, I actually am using the javac compiler.

'javac' is not a compiler, it is a command/executable which comes with all compiler implementations for java for compiling your source code. So irrespective of whether you are using the JDK from BEA, IBM, Sun, GCJ, Oracle etc most of them should give you a javac executable to compile your source code.

Since I guess you are on Linux, so you must be using the Ecipse Java Compiler in combination with the GCJ runtime. The fact that you are using GCJ to run your application is clear from this error in your first post:-

at gnu.java.lang.MainThread.run(libgcj.so.90)

To check which version of the compiler you are using shoot:

javac -version

Peter has mentioned is the correct way to run your programs, you mention the main class name only and leave out the ".class" extension.
Finally make it a point to follow a set of standard coding conventions from the start for naming your classes, packages, identifiers etc. Here is a link to the Sun Java Code Conventions or you could use this style guide from javaranch.com which is more suitable for beginners.

Thanks peter, you solved my immediate problem. However, thank you stephen for the amazingly informative post. I'll definitely have to go over the style guides you linked me to.

Anyways, when I try a "javac -version", I am informed that I am running the "Eclipse Java Compiler 0.894_R34x, 3.4.2 release". Would you recommend that I use a different compiler? If so, how would I go about doing that. Also, would you recommend that I compile/run my code from within an IDE (like Eclipse, Netbeans) or do it manually for now?

Thanks again.

The Eclipse java compiler is OK, The problem is your runtime which you are using 'GCJ', I would suggest you use the JRE from the Sun site directly which I had linked to in the first post. Remember there are two components which process your program currently:-
First the compiler which compiles your code to the class file (javac)
Second the JRE or the Java Virtual Machine which runs your programs (java).
The Eclipse compiler is fine, but since you are just starting out, rather than use your compiler and runtime from different third party vendors, I would suggest you stick to the original SUN JDK (which includes both of them) which I had linked in the first post.
Also to make it simpler to install do not download the ".rpm.bin" version of the jdk, instead use the ".bin" version.

Also if you are just starting Java then stick with the basic editors only like emacs or vim, once you are familiar with the ins and outs of Java then jump to Eclipse or Netbeans or even Peter's Favourite Intellij IDEA.

Also if you are just starting Java then stick with the basic editors only like emacs or vim, once you are familiar with the ins and outs of Java then jump to Eclipse or Netbeans or even Peter's Favourite Intellij IDEA.

There is nothing wrong with IntelliJ specially since there is community edition ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.