Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception in thread "main" java.lang.NoClassDefFoundError: wrong name

Tags:

My file directory:

project/src/m2mcom/entities/AutomatedTelnetClient.java            /web/Simple.java                       /org/apache/commons/net/telnet/TelnetClient.java  

The source code of the Simple.java:

package m2mcom.web; import m2mcom.entities.AutomatedTelnetClient; import java.util.*; import java.io.*;  public class Simple {     public static void main(String [] args) {         try {             AutomatedTelnetClient telnet = new AutomatedTelnetClient();             String answer = telnet.request();             System.out.println(answer);         } catch (Exception e) {             System.err.println("Error");         }     } } 

And when I execute Simple.class, without any errors of compilation, I get this error message:

C:\Users\Victor\Desktop\project2\src\m2mcom\web>java Simple Exception in thread "main" java.lang.NoClassDefFoundError: Simple (wrong name: m 2mcom/web/Simple) 

Does anyone know how to solve this?

like image 578
vicesbur Avatar asked Jan 25 '13 11:01

vicesbur


People also ask

What is error Java Lang NoClassDefFoundError?

java. lang. NoClassDefFoundError is runtime error thrown when a required class is not found in the classpath and hence JVM is unable to load it into memory.

Why am I getting a NoClassDefFoundError in Java?

The NoClassDefFoundError is a runtime error in Java that occurs if the Java Virtual Machine (JVM) or a ClassLoader instance attempts to load the definition of a class that could not be found. The class definition exists at compile-time but is not available at runtime.

Can we catch NoClassDefFoundError in Java?

In the case of NoClassDefFoundError, the class was present at compile time, but Java runtime could not find it in Java classpath during runtime.


1 Answers

You're executing the command in the wrong folder, with the wrong classname. You need to use the fully qualified name (FQN) when running a Java class. And of course, you have to be in the right directory. In your example, the FQN of your class is m2mcom.web.Simple (combination of the package m2mcom.web and the simple name Simple).

As far as deducing the right directory, your classes are stored in a hierarchical folder structure, which basically starts in C:\Users\Victor\Desktop\project2\src.

So to correctly execute your program, from C:\Users\Victor\Desktop\project2\src, do;

java m2mcom.web.Simple 
like image 81
Perception Avatar answered Oct 20 '22 16:10

Perception