Exception in thread “main” java.lang.NoClassDefFoundError: HelloWorld

前端 未结 4 1386
长情又很酷
长情又很酷 2020-12-13 11:46

I\'ve been working on this for about an hour and thumbing through Q&As on stackoverflow but I haven\'t found a proposed solution to my problem. I\'m sorry if this is a d

相关标签:
4条回答
  • 2020-12-13 11:59

    You've put your class in a package named "main", but you're trying to treat it like it isn't in a package. Since you put package main; at the top of your source file, you need to put HelloWorld.java in ./main, then run javac ./main/HelloWorld.java, followed by java -cp . main.HelloWorld.

    These commands will get you the working example you're 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
    
    0 讨论(0)
  • 2020-12-13 12:08

    package main;

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

    Java requires that package names should also be mirrored in the directory structure. This means that:

    1. Your HelloWorld.java file should be in a directory named main
    2. You should execute javac and java from the directory containing main, not from main itself
    3. The classpath should contain the directory where the main directory is, not main itself
    4. java expects the canonical name of the class to execute, so main.HelloWorld

    So, to recap:

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

    From myproject, run javac main/HelloWorld.java

    From myproject, run java -cp ./ main.HelloWorld

    0 讨论(0)
  • 2020-12-13 12:10

    As a beginner you might encounter a very similar scenario where the error output is the same. You try to compile and run your simple program(without having any package set) 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 your class name. To solve it just use

    javac HelloWorld.java
    java HelloWorld
    

    See the Java page - Lesson: Common Problems (and Their Solutions)

    0 讨论(0)
  • 2020-12-13 12:18

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

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

    Solution: you need to add the class or .jar file which contains this class into the java classpath. When you are running a java class from the command line, you need to add the dot (.)

    java YourSingleClass -cp .
    

    into the classpath which tells the JVM to search for classes in actual directory.

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

    java org.somepackage.SomeClass -cp myJarWithSomeClass.jar
    
    0 讨论(0)
提交回复
热议问题