Link to home
Start Free TrialLog in
Avatar of madhire
madhire

asked on

Exception in thread "main" java.lang.NoClassDefFoundError

when i run the java program HelloWorldApp i am getting the error -Exception in thread "main" java.lang.NoClassDefFoundError , i am new to java and i downloaded java in C: directory and set user and system varaible to -
USER VARAIBLE:
 %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Common Files\Ulead Systems\MPEG;C:\j2sdk1.4.2_09\bin

SYSTEM VARAIBLE:
 %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Common Files\Ulead Systems\MPEG;C:\j2sdk1.4.2_09\bin

SET CLASSPATH:
C:j2sdk1.4.2_09\bin\;%CLASSPATH%;

when i compile the programe there is no problem, i am seeing two files HelloWorldApp.java and helloWorldApp.class, when try this command-set CLASSPATH=, the prgrame is running and giveing result-HelloWorld.

the source code is:

/**
 * The HelloWorldApp class implements an application that
 * displays "Hello World!" to the standard output.
 */
public class HelloWorldApp {
    public static void main(String[] args) {
        // Display "Hello World!"
        System.out.println("Hello World!");
    }
}

pleasr response in urgent, i am wasting all my time to download diffrent versions.
Avatar of Dejan Pažin
Dejan Pažin
Flag of Austria image


CLASSPATH must include the classes your programm uses. Since you only have a HelloWorldApp class, the CLASSPATH should include that class.

There is one special rule: if you dont set the CLASSPATH, then the folder from which you are running the application is by default in the classpath. Thats why your programm runs if you do this: set CLASSPATH=

But if you do this:
SET CLASSPATH:C:j2sdk1.4.2_09\bin\;%CLASSPATH%;

then your CLASSPATH is set, and it doesnt include the class you are trying to run.

Basicaly, the simple solution to your problem is: dont have the CLASSPATH set in your environment. Set it only when you need it.
Setting classPath: Thouroughly read this: http://mindprod.com/jgloss/classpath.html
It contains some clear (Helloworld) examples
Avatar of rk_radhakrishna
rk_radhakrishna

Assume your program exists in  C:\work
then give CLASSPATH=%CLASSPATH%;C:\work;
Avatar of madhire

ASKER

hai dejanpazin

i removed set CLASSPATH from environmental varaibale but still i am getting the same error


I quote from the link I provided:

To run a HelloWorld.class app in C:\MyDir, use:

cd \MyDir
java.exe -classpath . HelloWorld    (HelloWorldApp, in your case)
Avatar of madhire

ASKER

hai zzynx.

i am don't understand what you are saying....will you explain me in clear. i stored the java in C:\j2sdk1.4.2_09 version..
Try this

javac -classpath filename.java
cd to the diurectory containing your class file (probably the one you compiled it from) and run it using:

java HelloWorld
ASKER CERTIFIED SOLUTION
Avatar of Dejan Pažin
Dejan Pažin
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> when i compile the programe there is no problem, i am seeing two files HelloWorldApp.java and helloWorldApp.class,
Suppose you have those files in a directory C:\MyJavaApps
Then
1) Open a Cmd prompt
2) type
       cd \MyJavaApps
3) type
       java -classpath . HelloWorldApp
Avatar of madhire

ASKER

hai zzyx,

i stored file in C:\j2sdk.14.2_09

then i gave command in cmd prompt like- c:\j2sdk1.4.2_09\java-classpath.HelloWorldApp but i amgetting the following error
java-classpath.HelloWorldApp' is not recognised as an internal or external comand
sorry, above I meant

java HelloWorldApp
Avatar of madhire

ASKER

hai dejanpazin ,

thanks for your suggestion, it's worked for me, i put classpath to empty then i restarted cmd prompt.

thanks
>> 3) type
>>       java -classpath . HelloWorldApp

Exactly as posted
Including all the spaces
>> i stored file in C:\j2sdk.14.2_09
Bad idea.
Better use a dedicated directory to store your source/class files in
We have a winner :-)