Java.lang.NoClassDefFoundError

I am learning Java, I have problems running an example program.

I have two files:

GoodDog.java:

class GoodDog { private int size; public int getSize() { return size; } public void setSize(int s) { size = s; } void bark() { if (size > 60) { System.out.println("Wooof! WoooF!"); } else if (size > 14) { System.out.println("Ruff! Ruff!"); } else { System.out.println("Yip! Yip!"); } } } 

GoodDogTestDrive.java:

  class GoodDogTestDrive { public static void main (String[] args) { GoodDog one = new GoodDog(); one.setSize(70); GoodDog two = new GoodDog(); two.setSize(8); System.out.println("Dog one: " + one.getSize () ); System.out.println("Dog two: " + two.getSize () ); one.bark(); two.bark(); } } 

They are printed exactly as they are in the book, and compiled without problems. When I try to start GoodDogTestDrive, I get the following:

 nephi-shields-mac-mini:/Developer/MyProjects/GoodDog nephishields$ java GoodDogTestDrive.class java.lang.NoClassDefFoundError: GoodDogTestDrive/class Exception in thread "main" nephi-shields-mac-mini:/Developer/MyProjects/GoodDog nephishields$ 

What am I doing wrong?

+6
source share
7 answers

Do not include .class in the command:

 java GoodDogTestDrive 
+14
source

It is important to note that although the resolution was simple for this case (simply by removing .class from the Java command line), java.lang.NoClassDefFoundError can be difficult to determine in the context of the development of Java web applications.

This Java exception means that the Java runtime class cannot be loaded and / or found in the current loader of the Thread class. This is usually the result:

  • There is no library in the path to the execution path.
  • Static {} block failure of code initializer execution (preventing the loading of the class of the affected class).
  • JAR file package / class loader tree problems, such as mixing JAR files deployed on the loader of the child and parent classes.

It is recommended that any Java beginner be able to correctly understand this type of problem while waiting for future troubleshooting episodes.

+11
source

just run the program using "java yourClass".

Do not use .class at the end.

Try it and let us know.

+1
source

If the GoodDog class file is not in the current working directory, you will need to set the class path in your java command ....

 java GoodDogTestDrive -cp ./path/to/gooddog/ 
0
source

Issue-java.lang.NoClassDefFoundError

The root cause . Incorrect Java path set in environment variable section

Solution : set the correct JAVA_HOME path

Steps-> Setting the environment variable (My comp-right click β†’ Properties-> Env Variable-> Advance tab β†’ Variable)

  • Create a new JAVA_HOME environment variable.

     JAVA_HOME **.;C:\Program Files (x86)\Java\jdk1.6.0_14** 
  • Set the JAVA_HOME variable in the PATH variable section.

     PATH %JAVA_HOME%\bin 
  • Set the JAVA_HOME variable in the CLASSPATH variable

     CLASSPATH %JAVA_HOME%\jre\lib 
  • Reboot system

  • Check the whole variable

     echo %CLASSPATH% echo %JAVA_HOME% echo %PATH% 
  • Compile java class javac Test.java

  • Run java test Java program

0
source

Thrown if a JVM or ClassLoader instance tries to load the class definition (method call or create a new instance) and the class definition cannot be found.

The reason for NoClassDefFoundError is that a particular class is not available at run time, but available at compile time.

1. The class refers to a missing JAR file, or a JAVA or JAR has not been added to the class path.

2. The class is not available in the Java Classpath.

3. NoClassDefFoundError in Java due to an exception in the Static Initializer block . when your class performs static initialization in a static block, and if the static block throws an Exception, the class that is referencing this class will get a NoclassDefFoundError in Java.

4.Your Classpath, PATH or JAVA_HOME is not configured correctly or the JDK installation is incorrect. which can be solved by reinstalling the JDK .

5.Paste maven clean-install and just restart the server with the necessary Source (.java) files.

0
source

I encountered the same problem as you.

I solved the problem as follows:

  • Check class path in environment variables
  • I opened the project in the eclipse navigator and I checked the class files. My class files were in different folders, not in the bin folder. I just copied the missing files to the bin folder, after which the problem was resolved.

Just copying the missing .class files to the bin directory of the eclipse project resolved the issue.

-1
source

Source: https://habr.com/ru/post/885457/


All Articles