exception in thread 'main' java.lang.NoClassDefFoundError:

JavaNoclassdeffounderror

Java Problem Overview


The following program is throwing error:

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

CLASSPATH C:\Program Files\Java\jdk1.6.0_18\bin\

Path C:\Program Files\Java\jdk1.6.0_18\bin\

JAVAHOME C:\Program Files\Java\jdk1.6.0_18\bin

Can you please tell me the root cause?

Java Solutions


Solution 1 - Java

I found one another common reason. If you create the java file inside a package using IDE like eclipse, you will find the package name on the top of your java file like "package pkgName". If you try to run this file from command prompt, you will get the NoClassDefFoundError error. Remove the package name from the java file and use the commands in the command prompt. Wasted 3 hours for this. -- Abhi

Solution 2 - Java

Exception in thread "main" java.lang.NoClassDefFoundError  

One of the places java tries to find your .class file is your current directory. So if your .class file is in C:\java, you should change your current directory to that.

To change your directory, type the following command at the prompt and press Enter:

cd c:\java  

This . tells java that your classpath is your local directory.

Executing your program using this command should correct the problem:
java -classpath . HelloWorld  

Solution 3 - Java

If your package is helloworld you would go to parent dir of your package then run:

java helloworld.HelloWorld

Solution 4 - Java

Run it like this:

java -jar HelloWorld.jar

Solution 5 - Java

See http://scottizu.wordpress.com/2013/08/28/fixing-the-exception-in-thread-main-java-lang-noclassdeffounderror-in-eclipse/.

This is the long form of the Java commands that can be run from a Windows command prompt:

"C:\Program Files\Java\jdk1.6.0_18\bin\javac.exe" -classpath "C:\Users\Scott\workspace\myproject" com\mycompany\myapp\HelloWorld.java
"C:\Program Files\Java\jdk1.6.0_18\bin\java.exe" -classpath "C:\Users\Scott\workspace\myproject" com.mycompany.myapp.HelloWorld
  1. These commands can be run from any directory, meaning you don't have to be in the directory where your HelloWorld.java file is.
  2. The first line compiles your HelloWorld.java file, creating a HelloWorld.class file.
  3. The second line runs the HelloWorld.class file.
  4. The -classpath tells java where to look for the specified file in each command
  5. The Java compiler (javac.exe) expects the location of the java file, relative to the classpath (ie the file is located at C:\Users\Scott\workspace\myproject\com\mycompany\myapp\HelloWorld.java)
  6. Java (java.exe) expects the package (ie com.mycompany.myapp) and class (HelloWorld), relative to the classpath (ie the file is located at C:\Users\Scott\workspace\myproject\com\mycompany\myapp\HelloWorld.class)

Notice the classpath has no slash at the end. The javac.exe commands expects the file to end with ".java". The java.exe command expects the full class name and does not end with ".class".

There are a few ways to simplify these commands:

  1. You don't have to specify the entire path to java.exe. Add Java to the Windows Path (Run->sysdm.cpl->Advanced Tab->Environment Variables->Select Path->Edit->Append ";C:\Program Files\Java\jdk1.6.0_18\bin"). Or you can append JAVA_HOME and create that Environment Variable.

  2. You don't have to enter the entire classpath (ie, you can just use -classpath "."). Enter the directory you will be working in:

    cd "C:\Users\Scott\workspace\myproject"

  3. You can use the default package (put the HelloWorld.java file directory in your working directory and don't use the Java package directive)

If you make these changes you would run something like this (and you might be able to leave out -classpath "."):

cd "C:\Users\Scott\workspace\myproject\"
javac -classpath "." HelloWorld.java
java -classpath "." HelloWorld

Solution 6 - Java

Here is what finally worked.

`@echo off
set path=%path%;C:\Program Files\Java\jdk1.7.0_71\bin;
set classpath=C:\Program Files\Java\jdk1.7.0_71\lib;
cd <packageDirectoryName>
javac .\trainingPackage\HelloWorld.java
cd ..
java trainingPackage.HelloWorld 
REM (Make sure you are on the parent directory of the PackageName and not inside the    Packagedirectory when executing java).`

Solution 7 - Java

The javadoc of NoClassDefFounError itself would be a good start (here), and then I'll suggest you clean and rebuild your project.

Solution 8 - Java

Your CLASSPATH needs to know of the location of your HelloWorld class also.

In simple terms you should append dot . (means current directory) in the CLASSPATH if you are running javac and java commands from DOS prompt.

Solution 9 - Java

The CLASSPATH variable needs to include the directory where your Java programs .class file is. You can include '.' in CLASSPATH to indicate that the current directory should be included.

set CLASSPATH=%CLASSPATH%;.

Solution 10 - Java

type the following in the cmd prompt, within your folder:

set classpath=%classpath%;.;

Solution 11 - Java

I had the same problem, and stumbled onto a solution with 'Build Main Project F11'. The ide brought up an "option" that I might want to uncheck 'Compile on Save' in the Build > Compiling portion of the Project configuration dialog. Unchecking 'Complile on Save' and then doing the usual (for me) 'Clean and Build' did the trick for me.

Solution 12 - Java

Java-File:

package com.beans;

public class Flower{
 .......
}

packages :=> com.beans,
java class Name:=> Flower.java,
Folder structure (assuming):=> C:\com\beans\Flower.*(both .java/.class exist here)

then there are two ways of executing it:

1. Goto top Folder (here its C:\>),
     then : C:> java com.beans.Flower 
2. Executing from innermost folder "beans" here (C:\com\beans:>),
     then: C:\com\beans:> java -cp ./../.. com.beans.Flower

Solution 13 - Java

Add the jar in your classpath.

For example, if you have NoClassDefFoundError for class XXXXXX, get the jar containing the class and add it to your classpath manually.

It works for me. Hopefully it will work for you as well.

Solution 14 - Java

The Problem here is the setting the environment and the running of the class file. a. To set the environment path run the following command: set path=C:\Program Files (x86)\Java\jdk1.7.0\bin b. run the program from the package like com.test.TestJavaClass

Command: java com.test.TestJavaClass

The general issue here is we run it either from inside the package like src/package/name. We should not include src, package name is enough.

Solution 15 - Java

Try doing

javac Hello.java

and then, if it comes up with no compiler errors (which it should not do because I cannot see any bugs in your programme), then type

java Hello

Solution 16 - Java

If you want to 'compile and execute' any java file that you have created using any IDE(like eclipse), just run the below commands:

> Compile: javac Users\dhiraj01\workspace\Practice\src\PracticeLogic\Logics.java

>Execute: java -classpath Users\dhiraj01\workspace\Practice\src\ PracticeLogic.Logics

Solution 17 - Java

if your Program.java is in "src/mypkg/subpkg/" directory:

go to "src" dir

Then to compile use "javac mypkg/subpkg/Program.java"

To run use "java mypkg.subpkg.Program.class"

Solution 18 - Java

I finally found this as a bug with Apache Netbeans editor:

Below steps will remove the error:

  1. Rename the filename & class to Abc
  2. Close the editor
  3. Reopen the editor
  4. Rename the filename & class, from Abc, back to the previous name
  5. Now debug project (Ctrl+F5) works fine

Hope that helps, if you are using new Apache Netbeans (not old Netbeans)

Solution 19 - Java

I had this proplem I used maven. I just click maven projects ant then name

project-> plugins -> clean -> and button run.

enter image description here

Solution 20 - Java

You can find information about required libraries inside pom.xml, it is much easier to use tools like Apache Maven to build java applications.

    <dependency>
		<groupId>org.yaml</groupId>
		<artifactId>snakeyaml</artifactId>
		<version>1.20</version>
	</dependency>
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-lang3</artifactId>
		<version>3.7</version>
	</dependency>
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-text</artifactId>
		<version>1.2</version>
	</dependency>
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-math3</artifactId>
		<version>3.6.1</version>
	</dependency>
	<dependency>
		<groupId>com.google.guava</groupId>
		<artifactId>guava</artifactId>
		<version>24.0-jre</version>
	</dependency>

	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-api</artifactId>
		<version>1.7.25</version>
	</dependency>
	<dependency>
		<groupId>com.google.inject</groupId>
		<artifactId>guice</artifactId>
		<version>4.2.0</version>
	</dependency>
	<dependency>
		<groupId>com.google.inject.extensions</groupId>
		<artifactId>guice-assistedinject</artifactId>
		<version>4.2.0</version>
	</dependency>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHarithaView Question on Stackoverflow
Solution 1 - JavaAbhiView Answer on Stackoverflow
Solution 2 - JavaSaurabh GokhaleView Answer on Stackoverflow
Solution 3 - JavahadibouView Answer on Stackoverflow
Solution 4 - JavaKonstantin SpirinView Answer on Stackoverflow
Solution 5 - JavaScott IzuView Answer on Stackoverflow
Solution 6 - JavaravikanthView Answer on Stackoverflow
Solution 7 - JavatalnicolasView Answer on Stackoverflow
Solution 8 - JavaanubhavaView Answer on Stackoverflow
Solution 9 - JavaAmir AfghaniView Answer on Stackoverflow
Solution 10 - JavaBruevinView Answer on Stackoverflow
Solution 11 - Javauser2676860View Answer on Stackoverflow
Solution 12 - JavaUdit NagiView Answer on Stackoverflow
Solution 13 - JavaGHULAM SARWARView Answer on Stackoverflow
Solution 14 - JavaChirag PahujaView Answer on Stackoverflow
Solution 15 - JavaDalekCaan99View Answer on Stackoverflow
Solution 16 - JavaDhiraj AggarwalView Answer on Stackoverflow
Solution 17 - Javamar915hView Answer on Stackoverflow
Solution 18 - JavaManohar Reddy PoreddyView Answer on Stackoverflow
Solution 19 - JavaPeter PetrovView Answer on Stackoverflow
Solution 20 - JavaRashmi ReddyView Answer on Stackoverflow