The dreaded java.lang.NoClassDefFoundError

余生颓废 提交于 2019-11-28 11:36:59

A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.

In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.

The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:

java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass

To save the effort of typing and editing the argument in commandline everytime, use a .bat file.

Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.

java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
David Tinker

Are you specifying the classpath to java on the command line?

$ java -cp lib/commons-lang-2.4.jar your.main.Class

The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.

If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:

java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar

If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).

Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with

java -jar yourProgram.jar

There are several variants depending on your needs.

Kelly S. French

Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See @BalusC's answer for good coverage of that.

Eclipse doesn't build executable java classes by default. Don't ask me why, but it probably has something to do with using their own tools.jar (somewhere in plugins/org.eclipse.core ?) so that Eclipse can run without a JDK.

You can usually go to your project bin directory and do:

java -cp . MyClass

But if you have external jars, Eclipse handles those internally in another weird way, so you'll need to add those too.

make sure your jar commons-lang-2.4.jar in classpath and not redudance.

I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth

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