How is NoClassDefFoundError thrown

蓝咒 提交于 2019-12-20 06:27:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!