aspose file tools*
The moose likes Beginning Java and the fly likes Exception in thread 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" java.lang.NoClassDefFoundError: Test" Watch "Exception in thread "main" java.lang.NoClassDefFoundError: Test" New topic
Author

Exception in thread "main" java.lang.NoClassDefFoundError: Test

Akshay D Joshi
Greenhorn

Joined: Aug 04, 2011
Posts: 9
Hello all,

____________________________
class Test
{
public static void main(String ar[])
{
System.out.println("This is Test Program");
}
}
___________________________

When i run the above program through cmd I get the Exception as following:
Exception in thread "main" java.lang.NoClassDefFoundError: Test

---------------------------------------


When i run the program through Eclipse IDE it runs correctly & gives me correct output......

But i want to understand why this happens.....
is there anything wrong with my cmd or is it because of any virus???
Angus Comber
Ranch Hand

Joined: Jul 16, 2011
Posts: 90
What does "When i run the above program through cmd" mean? What did you type at the command prompt to get this error?
Akshay D Joshi
Greenhorn

Joined: Aug 04, 2011
Posts: 9

Here is my Output of the program.......

D:\java>javac Test.java

D:\java>java Test
Exception in thread "main" java.lang.NoClassDefFoundError: Test

D:\java>
Angus Comber
Ranch Hand

Joined: Jul 16, 2011
Posts: 90
Ah should be

public class Test

Test has to be public - I guess if you don't define default could be private. not sure. Add public and it should work.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

It doesn't need to be public at all.

Akshay, what do you get when you execute echo %CLASSPATH%?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pradeep allada
Greenhorn

Joined: Jul 28, 2011
Posts: 17
Angus Comber wrote:Ah should be

guess if you don't define default could be private. .

default access specifier is default only not the private

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

It has nothing to do with public vs. private; it's a classic first program problem. Something has set the CLASSPATH environment variable on your system; this tells Java where to look for .class files. The CLASSPATH setting does not, apparently, include "." (dot), which would stand for the current directory. The best way to deal with this is to tell Java where to look right when you execute the program:

java -cp . Test

(That's java space dash cp space dot space Test.)

[Jess in Action][AskingGoodQuestions]
Angus Comber
Ranch Hand

Joined: Jul 16, 2011
Posts: 90
I beg to differ

Try this:

public class Test
{
public static void main(String[] ar)
{
System.out.println("This is Test Program");
}
}

Then try omitting the public before test - get error as noted by poster.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

I never got an error that way. What JVM are you using, Oracle's or a different one?
Angus Comber
Ranch Hand

Joined: Jul 16, 2011
Posts: 90
Probably the same JVM you are using. Latest version for Windows. "1.6.0_26"

See page 8 Head first Java. They have an example, public class MyFirstapp etc Note says public so everyone can access it.

Although having said all that both version run ok for me.

I was compiling at one stage using

java ../classes/Test and then getting
Exception in thread "main" java.lang.NoClassDefFoundError: //\classes\Test
Caused by: java.lang.ClassNotFoundException: ..\classes\Test
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: ..\classes\Test. Program will exit.

But if I ran directly in the classes folder it ran without a problem.

Where I saw the problem I was in source - which is on same level as classes.

Why am I seeing this behaviour?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Angus Comber wrote:
Why am I seeing this behaviour?


Because the argument to "java.exe" is not a path, it's the name of a class. java.exe must find the class by looking along the CLASSPATH. If you were to have typed

java -cp ../classes Test

it would have worked just fine.

The name of a class includes the name of the package, if any; if you try to run java Foo.class, Java complains that there's no class named Foo.class. There's only a class named Foo, in a file named Foo.class.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Angus Comber wrote:Note says public so everyone can access it.



"Everyone" here means "code in other packages." For a class in package A to be visible to a class in package B, it must be marked public.

It has nothing to do with whether java.exe can use it or not, as you've already discovered.
Angus Comber
Ranch Hand

Joined: Jul 16, 2011
Posts: 90
Ah ok, thanks. java -cp ..classes Test works.

Didn't know about -cp, thanks.

Akshay D Joshi
Greenhorn

Joined: Aug 04, 2011
Posts: 9
Hi all...

the following is working.....

D:\java>java -cp . Test
This is Test Program

But what should i add to my classpath....
I am using jdk 1.6...

i have set environment variable "path" as "C:\Program Files\Java\jdk1.6.0\bin"...
and "classpath" ass "C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar" for servlets....


Is there anything i need to add in environment variables
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If you must set CLASSPATH, then include "." as an entry. But it's best to simply unset it altogether; "." is the default.
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Thanks Ernest Friedman-Hill for telling one more way to deal with CLASSPATH i.e you told
java -cp . FileName.......................Thanks........But how come java knows about our class file using this on our cmd??Because we haven't mentioned the directory the folder in which our class file is located???
Angus Comber
Ranch Hand

Joined: Jul 16, 2011
Posts: 90
Rameshwar, have a think about your question. It doesn't. By using -cp . <Filename> you are telling the JVM that your classes are in the current directory. . means current directory.

Generally, you would not use java -cp . <file> because it is equivalent to java <file> which is obviously less typing. But the original poster (maybe that is you) had a problem with their classpath. So -cp is a switch which overrides the environment set CLASSPATH so you can just tell it - my java class is here.

What the original poster should of course do is fix their CLASSPATH problem.

The important thing to remember is that you pass the class name to the java command. You can use -cp on the command line to tell the JVM which folder to look for your class files. OR you can set the folder to look in using the CLASSPATH environment variable.

Hope this makes sense.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exception in thread "main" java.lang.NoClassDefFoundError: Test