tags:

views:

52

answers:

2

All,

My classpath has been set to the following folder:

CLASSPATH = .;C:\Program Files\Java\jdk1.6.0_21\bin;C:\Program Files\Java\jdk1.6.0_21\bin\project

All my java files and class files are under project folder listed in CLASSPATH.

Yet I am getting the following error while running a DriverClass in project folder:

C:\Program Files\Java\jdk1.6.0_21\bin\project>java DriverClass
Exception in thread "main" java.lang.NoClassDefFoundError: DriverClass (wrong name: project/DriverClass)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: DriverClass.  Program will exit.

Can anyone please help me understand the reason here?

+2  A: 

Try:

C:\Program Files\Java\jdk1.6.0_21\bin>java project.DriverClass

assuming your package is project

RC
tried it ... still getting the error
darkie15
Try adding `-cp .` as BalusC said.
RC
Thank you RC ..
darkie15
+3  A: 

You're inside the package. You should be sitting in the package root. Go one folder up.

cd ..

Then reexecute it using java project.DriverClass.


That said, you should prefer not to use the CLASSPATH environment variable. Yours is currently also invalid. There are spaces in unquoted pathnames. Also, the JDK/bin folder isn't supposed to go in the classpath. Just use the -cp argument like java -cp . project.DriverClass (while sitting in the package root folder).

BalusC
Thank you so much BalusC. Can you please tell me the correct setup for environment variables after installing java. I am tired of spending too much time on this trivial thing
darkie15
You're welcome. There are only 2 things: 1) full path to `JDK/bin` should be appended to OS-specific `%PATH%` environment variable so that you'll be able to execute `java.exe`, `javac.exe`, `javap.exe` and others from everywhere. As far now this looks already be properly setup. 2) full path to `JDK` (not `/bin`!) should go in `%JAVA_HOME%` environment variable. It's not necessary when you're still fiddling in command prompt, but most Java apps which require the JDK toolset (i.e. the compiler) are relying on this, e.g. Apache Tomcat webserver, Ant build tool, etc.
BalusC