• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't need to be public at all.

Akshay, what do you get when you execute echo %CLASSPATH%?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Angus Comber
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never got an error that way. What JVM are you using, Oracle's or a different one?
 
Angus Comber
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah ok, thanks. java -cp ..classes Test works.

Didn't know about -cp, thanks.

 
Akshay D Joshi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you must set CLASSPATH, then include "." as an entry. But it's best to simply unset it altogether; "." is the default.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Can't .... do .... plaid .... So I did this tiny ad instead:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic