exception in thread 'main' java.lang.NoCla

2020-01-25 05:45发布

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?

22条回答
女痞
2楼-- · 2020-01-25 06:01

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)

查看更多
叼着烟拽天下
3楼-- · 2020-01-25 06:02

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

查看更多
看我几分像从前
4楼-- · 2020-01-25 06:02

I spent four hours trying various permutations & search suggestions.

At last, found this post that worked but not the best solution to change the original code to test it.

  1. Tried changing CLASSPATH, to include class libraries, set classpath=JDKbin;JDKlib;JREbin;JRElib;myClassLib;.
  2. Changed the current directory to parent directory(package folder) and tired java <packageName>.<className> also tried the java ..\<packageName>.<className>

Neither worked.

However first response alone worked. Thank you so much Abhi!!!

"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"

Curious if there is any other way to make it work without having to change the original code? Thank you!

查看更多
再贱就再见
5楼-- · 2020-01-25 06:03

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).`
查看更多
趁早两清
6楼-- · 2020-01-25 06:06
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  
查看更多
孤傲高冷的网名
7楼-- · 2020-01-25 06:06

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

set classpath=%classpath%;.;
查看更多
登录 后发表回答