58

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?

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Haritha
  • 621
  • 2
  • 6
  • 4
  • 3
    I doubt this is the problem, but JAVA_HOME should not include `bin`. On my system, I do: JAVA6_HOME=c:\sdks\jdk1.6.0_18 JAVA_HOME=%JAVA6_HOME% PATH=%PATH%;%JAVA_HOME%\bin – Dilum Ranatunga Jun 13 '11 at 22:44

20 Answers20

73

I found one other common reason. If you create the java file inside a package, using IDE like Eclipse, you will find the package name at 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.

jsotola
  • 2,238
  • 1
  • 10
  • 22
Abhi
  • 739
  • 5
  • 2
  • 10
    But how can i run it from the command prompt without removing the package name, i need the package name – OopsUser Aug 24 '15 at 14:25
28
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  
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
15

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

java helloworld.HelloWorld
hadibou
  • 328
  • 3
  • 8
12

Run it like this:

java -jar HelloWorld.jar
Konstantin Spirin
  • 20,609
  • 15
  • 72
  • 90
5

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
Scott Izu
  • 2,229
  • 25
  • 12
4

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).`
ravikanth
  • 151
  • 1
  • 10
3

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

talnicolas
  • 13,885
  • 7
  • 36
  • 56
  • I just performed a clean and build on my project, and it worked, I got this error when trying to debug, but a simple clean and build in NetBeans IDE 8.1 worked. Try this first. – Omar Ayala Nov 13 '16 at 05:57
2

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.

Daniele Licitra
  • 1,520
  • 21
  • 45
1

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

set classpath=%classpath%;.;
Marty McVry
  • 2,838
  • 1
  • 17
  • 23
Bruevin
  • 11
  • 1
1

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.

1

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
Udit Nagi
  • 21
  • 2
1

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
1

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%;.
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
0

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.

Chirag Pahuja
  • 31
  • 1
  • 6
0

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

DalekCaan99
  • 51
  • 1
  • 8
0

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

Dhiraj Aggarwal
  • 118
  • 1
  • 10
0

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"

mar915h
  • 33
  • 3
0

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)

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
0

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

RKRK
  • 1,284
  • 5
  • 14
  • 18
0

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>