File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Exception in thread main() error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception in thread main() error" Watch "Exception in thread main() error" New topic

Exception in thread main() error

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 386
C:\Program Files\Java\jre6>java C:\Users\nir\Desktop\JavaFiles\Test

Exception in thread "main" java.lang.NoClassDefFoundError: C:\Users\nir\Desktop\JavaFiles\Test

I have setup class path , Test.class exists in specified path. And I am executing Test.class by providing absolute path.

Why am I getting such an error ? What is wrong in here ? I understand, that command is unable to locate Test.class file, but why ?
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

Don't try to give an absolute path for the class: there is no such class as "C:\Users\nir\Desktop\JavaFiles\Test". Instead, give the (fully-qualified, if using packages) name of the class, and use a -cp argument to give the location.
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 386
what is a fully qualified path ? Hopw is it different from absolute path ? Whats wrong with the way path is given ?

I dont see java executable file in path "C:\Program Files\Java\jre6\bin". I see java application file in this path, which is 142 kb
I see javac executable file and java application file in path "C:\Program Files\Java\jdk1.6.0_27\bin".

How will the class file be executed ? Does it execute with java application file ? Shall I just provide "C:\Program Files\Java\jdk1.6.0_27\bin" in the Path environment variable ? Or shall I provide both the paths mentioned in Path environment variable ?

John Jai

Joined: May 31, 2011
Posts: 1776
Use the command like java -cp <your class file location> <fully qualified path>

say if Test class is inside a package "com" then use like java -cp C:/.../Classes com.Test

If you see there is no java exe in the path program files folder as you mentioned, some other java exe that is pointed to by the %path% variable should be used for executing.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15091

What you pass to the java command is not a path name, but a class name. The string "C:\Users\nir\Desktop\JavaFiles\Test" is not a valid class name.

Try this (assuming that your Test class does not have a package statement):

java -cp C:\Users\nir\Desktop\JavaFiles Test

Note: C:\Users\nir\Desktop\JavaFiles should be the directory that contains the file Test.class.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 386
I had jdk6 and jre6 both installed at same level. (first I had installed jre an then jdk). It was working fine then.

I removed jre6 (It still exists under jdk6), the one which was installed first.

Now when I am running a class file , I am getting error

C:\Users\nir>java Test
Error: could not open `C:\Program Files\Java\jre6\lib\i386\jvm.cfg'

Path variable : C:\Program Files\Java\jdk1.6.0_27\jre\bin;C:\Program Files\Java\jdk1.6.0_27\bin;

I have tried different combinations but its not working. Why is that ? I never feel confident while setting Path because of this. I dont understand , sometimes jvm works fine, other times it does not.

Please provide some layman explanation.


Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46367
Is that the whole of your PATH? What about things like the system and win32 folders? Are they not in your PATH?
Try echo %PATH% at a command-line and see what happens.
Does the \jre\ folder still exist? [But the computer will probably do nothing if you put a non-existent folder into your PATH.]

It would appear there is a configuration file which has vanished. Try exploring the folder it is supposed to be in (C:\Program Files\Java\jre6\lib\i386).

You might find a new installation easier than all this messing about with folders. You will have to edit your PATH to include the Java™ installation folder, and to remove any old Java™ installations. The details are in the Java™ installation instructions (§4, I think).
Joydeep Ghatak
Ranch Hand

Joined: Sep 30, 2009
Posts: 41

Hi Nirjari,

As correctly pointed out by Ritchie, it will be good to have a fresh installation of the Java(JDK and JRE both).

Some points,

1. Please uninstall your current java installtion and re-install fresh java
2. you can check the following links along with the "installation instructions (§4, I think)" as correctly pointed out by Ritchie,

3. Also if the program is NOT very large, it will be good if you can let us show the program once.
4. Are you going to compile and execute the program using "command prompt" (cmd) ?
5. Please let us know the PATH and CLASSPATH values.

“Men are only as good as their technical development allows them to be.”
I agree. Here's the link:
subject: Exception in thread main() error
It's not a secret anymore!