Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld

I have been working on this for about an hour and browsing through Q & As in stackoverflow, but I have not found a suggested solution to my problem. Sorry if this is a duplicate, but I could not find a duplicate question with an answer that solved my specific problem.

I am trying to write and compile a java program from the terminal for the first time (until this moment I used Eclipse for java and VIM for everything else, but I feel that the time is completely switching to VIM). Here is my current HelloWorld code:

package main; public class HelloWorld { public static void main(String args[]) { System.out.println("Hello World!"); } } 

I compile and run using the following commands (specifying the class path to make sure this is not a problem):

 javac -cp "./" HelloWorld.java java -cp "./" HelloWorld 

This gives me the following error message:

 Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: main/HelloWorld) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:791) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480) 

I know that he sees the HelloWorld.class file and tries to access the HelloWorld class, because if I change the start command to:

 java -cp "./" Foo 

I get a completely different error message:

 Error: Could not find or load main class Foo 

I tried dozens of pages for troubleshooting and came to a minimum, including the following:

Exception in thread "main" java.lang.NoSuchMethodError: main

http://introcs.cs.princeton.edu/java/15inout/mac-cmd.html

java -version gives:

 java version "1.7.0_07" Java(TM) SE Runtime Environment (build 1.7.0_07-b10) Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode) 

My operating system is LinuxMint and uname -a gives:

 Linux will-Latitude-D620 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux 
+4
source share
4 answers

main package;

This means that your class is in the main package, and its canonical name is main.HelloWorld .

Java requires that package names also appear in the directory structure. It means that:

  • Your HelloWorld.java file should be in a directory named main
  • You should execute javac and java from the directory containing main , and not from main
  • The class path must contain the directory where the main directory is located, not main
  • java expects the canonical class name to execute, so main.HelloWorld

So, to repeat:

You should have something like myproject/main/HelloWorld.java

From myproject run javac main/HelloWorld.java

From myproject run java -cp ./ main.HelloWorld

+20
source

You have placed your class in a package named "main", but you are trying to process it as if it were not in the package. Since you put package main; at the beginning of the source file, you need to put HelloWorld.java in. / main, then run javac ./main/HelloWorld.java and then java -cp . main.HelloWorld java -cp . main.HelloWorld .

These commands will give you a working example that you are trying to build:

 mkdir main echo 'package main; public class HelloWorld { public static void main(String... args) { System.out.println("Hello World"); } }' > main/HelloWorld.java javac main/HelloWorld.java java -cp . main.HelloWorld 
+9
source

As a newbie, you may come across a very similar scenario where the error output is the same. You try to compile and run your simple program (without any set of packages), and you do this:

 javac HelloWorld.java java HelloWorld.class 

This will give you the same java.lang.NoClassDefFoundError, since java thinks HelloWorld is your package and class is the class name . To solve this problem just use

 javac HelloWorld.java java HelloWorld 

See Java Page - Lesson: Common Problems (and Solutions)

+2
source

Problem: Basically, the exception in the thread "main" java.lang.NoClassDefFoundError :

means that the class you are trying to run was not found in the classpath.

Solution : you need to add a class or .jar file that contains this class in the java class path. When you run the Java class from the command line, you need to add a period (.)

 java YourSingleClass -cp . 

to the class path, which tells the JVM to search for classes in the actual directory.

If you are using a class from a .jar file, you need to add this jar file to the classpath:

 java org.somepackage.SomeClass -cp myJarWithSomeClass.jar 
0
source

All Articles