This discussion is archived
3 Replies Latest reply: Apr 20, 2010 6:16 AM by 800560 RSS

Setting the classpath

843810 Newbie
Currently Being Moderated
Hi

Iam new to this language. I installed jdk in my system. And i have set the classpath in environment variables in PATH sytem variable and the path is C:\Program Files\Java\jdk1.6.0_20\bin. Now if i execute the program from my working directory it is fine. But if i execute the program from different path it is failing.

The steps from the command prompt are:

1) C:\Documents and Settings\Archana>javac ..\..\pgms\Hello.java
Compilation is working.
2) C:\Documents and Settings\Archana>java ..\..\pgms\Hello
While running the program it is scolding me for NoclassdeffoundError.

I need your help to solve this.

-- Archana
  • 1. Re: Setting the classpath
    EJP Guru
    Currently Being Moderated
    Iam new to this language. I installed jdk in my system. And i have set the classpath in environment variables
    Unnecssary
    in PATH system variable
    Necessary, but you do realize that CLASSPATH is different from PATH?
    1) C:\Documents and Settings\Archana>javac ..\..\pgms\Hello.java
    Compilation is working.
    What's the package statement inside Hello.java?

    The directory containing the first part of the package name is where you should be running javac and java from.
  • 2. Re: Setting the classpath
    843810 Newbie
    Currently Being Moderated
    Hi,

    Thanks for your reply.

    The program is executing successfully when giving its command from its current directory. Suppose the file is in C:pgrms directory and iam executing from C:\pgms> it is fine. If i try to compile the program by giving the command C:\>javac pgms\Hello.java it is getting compiled but when i execute it by giving the command C:\>java pgms\Hello it is giving java.lang.NoClassDefFoundError.


    Thanks
    Archana
  • 3. Re: Setting the classpath
    800560 Newbie
    Currently Being Moderated
    Archana1604 wrote:
    If i try to compile the program by giving the command C:\>javac pgms\Hello.java it is getting compiled but when i execute it by giving the command C:\>java pgms\Hello it is giving java.lang.NoClassDefFoundError.
    The required argument for the java.exe application launcher is the fully qualified (and case sensitive) class name. It is not a file or path. Fully qualified class name means the package name plus the class name. If your source code starts with for example "package some.pack;" then the fully qualified class name is some.pack.Hello but if there is no package statement, then the fully qualified class name is the same as the class name - Hello.

    If you go here [http://java.sun.com/javase/6/docs/technotes/tools/index.html] and go to General Information, you can find links for setting Classpath and How Classes Are Found. It is recommended to to use the -cp command line switch to tell java where to look for classes. In your case, assuming no package name, "java -cp pgms Hello" or "java -cp c:/pgrms Hello"

    There's a tutorial on packages here [http://java.sun.com/docs/books/tutorial/java/package/index.html]