问题
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.
回答1:
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).
回答2:
Go to F:\workspace\helloWorld\bin\ and run it this way:
java -cp .; com.hello.HelloWorld
来源:https://stackoverflow.com/questions/12909767/how-is-noclassdeffounderror-thrown