Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes error because of classpath? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "error because of classpath?" Watch "error because of classpath?" New topic
Author

error because of classpath?

Akshay Reddy
Greenhorn

Joined: May 14, 2009
Posts: 21
hello everyone,

my program:

class Example
{
public static void main(String args[])
{

System.out.println("Hello java");
}
}

Execution:

C:\Users\ABHINAV\Desktop>javac Example.java

C:\Users\ABHINAV\Desktop>java Example
Exception in thread "main" java.lang.NoClassDefFoundError: Example
Caused by: java.lang.ClassNotFoundException: Example
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: Example. Program will exit.

Information:
1) I installed jdk 6 update 13 with netbeans 6.5.1
2) i donot want to work with netbeans as of now.
3) so i set the classpath , edited the program in a Word pad( Example.java).
4) I feel when i bought the laptop i got a JRE with update 5 and my jdk which i installed yesterday got me a JRE update 13.
5) my program is compiling and not running , so i think it is using the compiler of sdk update 13,but running with update 5. but that should not make a problem right?
6) How to set the class path?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Lots of people will tell you all sorts of nonsense about classpaths, eg that you have to set a system classpath. You don't. Java works beautifully if there is no classpath set, but there are some applications which set a classpath for themselves, in which case you get that sort of error.
Of course, if you set a classpath to a particular directory, Java will only work if your files are in that one directory

Try java -cp . Example

If that works, delete whatever you put in the classpath variable, and replace it with . for current directory. Remember you have to separate classpath entries with ; on Windows.

A NoClassDefFoundError is not caused by incompatible Java versions; that would cause an UnsupportedClassVersionError. Try the following instructions, to see which version you are actually using:

java -version
javac -version

If you get 1.6.something for both, you are all right. Remember the type of "java" or "javac" program you are using depends on which appears first in the PATH variable. You do usually have to set a PATH variable.

Don't use Notepad and don't use a word processor, for programming. Notepad is too basic, and word processors might create "control characters" or “smart quotes” like this “” which the compiler can't "read." Try jEdit, Notepad2 or Notepad++ which (despite their names) are much much better than Notepad, and designed for programming.
Akshay Reddy
Greenhorn

Joined: May 14, 2009
Posts: 21
Its still not working..


output:
C:\Users\ABHINAV\Desktop>javac Example.java

C:\Users\ABHINAV\Desktop>java Example
Exception in thread "main" java.lang.NoClassDefFoundError: Example
Caused by: java.lang.ClassNotFoundException: Example
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: Example. Program will exit.

I suppose i messed up my environment variables
My environment variables are:

CLASS PATH
C:\Program Files\Java\jre1.6.0_05\lib\ext\QTJava.zip1;%JAVA_HOME%

JAVA_HOME ( a new variable i put in system variables)
C:\Program Files\Java\jdk1.6.0_13\bin

Path( path variable value in ystem variables)
%JAVA_HOME%

i downloaded notepade++ but still the same error aboe persists
Thank you for your reply.
Akshay Reddy
Greenhorn

Joined: May 14, 2009
Posts: 21
i am sorry the classpath value above is

CLASSPATH
C:\Program Files\Java\jre1.6.0_05\lib\ext\QTJava.zip;// i donot understand y i have a zip file there..and when i unzip it i get errors.

not the one there
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Abhinav,

In CLASSPATH , add "." at the end of the CLASSPATH. remember to add at the end.




Lots of people will tell you all sorts of nonsense about classpaths, eg that you have to set a system classpath. You don't. Java works beautifully if there is no classpath set, but there are some applications which set a classpath for themselves, in which case you get that sort of error.


Any java programers first step is to understand the CLASSPATH , so its best to understand it before plunging into the IDEs.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Balu Sadhasivam wrote:Any java programers first step is to understand the CLASSPATH , so its best to understand it before plunging into the IDEs.
You can program nicely without even knowing there is such a thing as a classpath.

The mention of QT suggests it comes from QuickTime which is a media player; other people on this forum have said QuickTime has a habit of altering your classpath without your knowing. It sets up its own classpath without a . in, so your access to "current directory" is lost.

You have been told twice to add a . to your classpath; have you tried that?
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Campbell Ritchie wrote:
Balu Sadhasivam wrote:Any java programers first step is to understand the CLASSPATH , so its best to understand it before plunging into the IDEs.
You can program nicely without even knowing there is such a thing as a classpath.

oh yeah , its like to miss a page in the book.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Balu Sadhasivam wrote:oh yeah , its like to miss a page in the book.
Only the introduction and you know nobody ever reads introductions
Akshay Reddy
Greenhorn

Joined: May 14, 2009
Posts: 21
awesome guys ,got it when i added a . at the end....
y is that i should a dot at the end ( just curious)

thank you
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

ABHINAV ATLA wrote:awesome guys ,got it when i added a . at the end....
y is that i should a dot at the end ( just curious)

thank you


it can be anywhere .. i stressed it , just to make sure that you dont overwrite other paths.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
The . means "try in the directory the command prompt is pointing to at the moment for any .class files" or shorter "current directory." If there is no set classpath, java will default to "current directory" otherwise it looks in the classpath and was in your case led astray.
Akshay Reddy
Greenhorn

Joined: May 14, 2009
Posts: 21
so is it like
if i set the classpath

1)i can place my java program anywhere in the system
2)use my command prompt to go the directory the program is in and run it.

and if i donot set the classpath

1)i cannot place the java program anywhere i wish. ( then were should the program be placed, with out setting the classpath).

Correct me
thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Please read the earlier posts. I don't think you have understood it correctly.

If you don't have a classpath, leave well alone.
If you already have a classpath (and maybe QuickTime won't work without it), add a . dot for current directory to it, separated from the other entries (on Windows/DOS) by ; semicolons.

Use the cd command to take your command line to wherever you have your Java programs.

Or, another way. Make a new directory.

  • Open a new command line.
  • mkdir Java
  • cd Java
  • Now you have a "Java" directory probably in "My Documents" and you can go to it by opening a new command prompt and writing "cd Java".

    Akshay Reddy
    Greenhorn

    Joined: May 14, 2009
    Posts: 21
    hey i got it, i did understand where i am supposed to add the . thing, i was confused regarding what are these environment variables classpath, path...etc are for...
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38508
        
      23
    PATH tells the Operating System where to look for programs invoked from the command line.
    CLASSPATH tells those programs where to look for files they require to run.
    Akshay Reddy
    Greenhorn

    Joined: May 14, 2009
    Posts: 21
    thank you

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38508
        
      23
    You're welcome
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: error because of classpath?