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条回答
Root(大扎)
2楼-- · 2020-01-25 06:10

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

查看更多
地球回转人心会变
3楼-- · 2020-01-25 06:13

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
查看更多
4楼-- · 2020-01-25 06:14

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
查看更多
【Aperson】
5楼-- · 2020-01-25 06:14

I had this error because I had my files within a package. So my foo package I had to call like:

java foo.HelloWorld

查看更多
登录 后发表回答