Tao Huang
Tao Huang

Reputation: 1291

How is NoClassDefFoundError thrown

I built a package called "com.hello" in eclipse and I wrote an easy HelloWorld program. Eclipse automatically added "package com.hello;" on top of my program. And HelloWorld.java was put in

F:\workspace\helloWorld\src\com\hello;

HelloWorld.class was put in

F:\workspace\helloWorld\bin\com\hello.

It worked very well in Eclipse. But when I entered the directory "F:\workspace\helloWorld\bin\com\hello" and used command line with "java HelloWorld," I got NoClassDefFoundError. I know it may have something to do with the classpath. But I'm not quite sure.

Upvotes: 0

Views: 150

Answers (2)

Jesper
Jesper

Reputation: 206796

Your class is in a package com.hello. To run it, you must make sure the base directory of the package, which is F:\workspace\helloWorld\bin in your case, is in the classpath.

Try running it like this:

java -cp F:\workspace\helloWorld\bin com.hello.HelloWorld

You can also go to the directory F:\workspace\helloWorld\bin and then run it with

java com.hello.HelloWorld

This will work because Java will use the current directory as the default (if you do not have the CLASSPATH environment variable set).

Upvotes: 4

Dan D.
Dan D.

Reputation: 32391

Go to F:\workspace\helloWorld\bin\ and run it this way:

java -cp .; com.hello.HelloWorld

Upvotes: 0

Related Questions