Java.lang.NoClassDefFoundError in command prompt run
This type of error is due to the class not found in the Classpath during runtime but found during compile time.
Look to print
System.getproperty("java.classpath")
which will print the classpath so you get an idea as to the actual runtime classpath.
Also, make sure you pass the fully qualified name of the class to "java" command which contains the main method for execution.
directory_that_holds_package>java package_name.Class_name
NoClassDefFoundError: wrong name
Exception in thread "main" java.lang.NoClassDefFoundError: ClientREST
So, you ran it as java ClientREST
. It's expecting a ClientREST.class
without any package
.
(wrong name: clientrest/ClientREST)
Hey, the class is trying to tell you that it has a package clientrest;
. You need to run it from the package root on. Go one folder up so that you're in the folder which in turn contains the clientrest
folder representing the package and then execute java clientrest.ClientREST
.
You should not go inside the clientrest
package folder and execute java ClientREST
.
How can I solve java.lang.NoClassDefFoundError?
After you compile your code, you end up with .class
files for each class in your program. These binary files are the bytecode that Java interprets to execute your program. The NoClassDefFoundError
indicates that the classloader (in this case java.net.URLClassLoader
), which is responsible for dynamically loading classes, cannot find the .class
file for the class that you're trying to use.
Your code wouldn't compile if the required classes weren't present (unless classes are loaded with reflection), so usually this exception means that your classpath doesn't include the required classes. Remember that the classloader (specifically java.net.URLClassLoader
) will look for classes in package a.b.c in folder a/b/c/ in each entry in your classpath. NoClassDefFoundError
can also indicate that you're missing a transitive dependency of a .jar file that you've compiled against and you're trying to use.
For example, if you had a class com.example.Foo
, after compiling you would have a class file Foo.class
. Say for example your working directory is .../project/
. That class file must be placed in .../project/com/example
, and you would set your classpath to .../project/
.
Side note: I would recommend taking advantage of the amazing tooling that exists for Java and JVM languages. Modern IDEs like Eclipse and IntelliJ IDEA and build management tools like Maven or Gradle will help you not have to worry about classpaths (as much) and focus on the code! That said, this link explains how to set the classpath when you execute on the command line.
NoClassDefFoundError when running a java class through the command prompt
@caius-brindescu is right, you have to specify the class like com.company.Main but also, you have to execute the class from inside the bin directory. So, run 'cd bin' and then your command and it should work.
This is like this because your PATH starts with a dot '.' which means here in this directory.
Got NoClassDefFoundError: wrong name with Windows path
I think you have to change your URL to point to classpath and load class by package name, like this
URLClassLoader mycl = new URLClassLoader(new URL[] {
new URL("file:///D:/Users/XXX/Desktop/sem-material-job/sem-material-job/sem-material-freemarkerjob/target/classes/") });
Class c2 = mycl.loadClass("com.ctrip.market.sem.freemarkerjob.dynamicjava.TEST123");
Related Topics
List All Files in the Folder and Also Sub Folders
Annotation to Filter Results of a @Onetomany Association
No Serializer Found for Class Org.Hibernate.Proxy.Pojo.Bytebuddy.Bytebuddyinterceptor
How to Change the Color of Titlebar in Jframe
Execute Jsp Directly from Java
What Does Inputstream.Available() Do in Java
Building an Uberjar with Gradle
Constantly Update UI in Java Fx Worker Thread
Running a Java Program as an Exe in Windows Without Jre Installed
Java Serialization - Java.Io.Invalidclassexception Local Class Incompatible
CSV File with "Id" as First Item Is Corrupt in Excel
Prevent Swing Gui Locking Up During a Background Task
How to Add Button in a Row of Jtable in Swing Java
Right Way to Write JSON Deserializer in Spring or Extend It
Java: Jackson Polymorphic JSON Deserialization of an Object with an Interface Property