Error: Could not find or load main class

This question already has an answer here:

  • What does “Could not find or load main class” mean? 39 answers

  • You must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath. Note that the windows classpath separator is a semi-colon ie ;


    If the class is in a package

    package thepackagename;
    
    public class TheClassName {
      public static final void main(String[] cmd_lineParams)  {
         System.out.println("Hello World!");
      } 
    }
    

    Then calling:

    java -classpath . TheClassName
    

    results in Error: Could not find or load main class TheClassName . This is because it must be called with its fully-qualified name:

    java -classpath . thepackagename.TheClassName
    

    And this thepackagename directory must exist in the classpath. In this example, . , meaning the current directory, is the entirety of classpath. Therefore this particular example must be called from the directory in which thepackagename exists.

    To be clear, the name of this class is not TheClassName , It's thepackagename.TheClassName . Attempting to execute TheClassName does not work, because no class having that name exists. Not on the current classpath anyway.

    Finally, note that the compiled (.class) version is executed, not the source code (.java) version. Hence “CLASSPATH.”


    You can try these two when you are getting the error: 'could not find or load main class'

    If your class file is saved in following directory with HelloWorld program name d:sample

  • java -cp d:sample HelloWorld
  • java -cp . HelloWorld
  • 链接地址: http://www.djcxy.com/p/9366.html

    上一篇: Apache Camel究竟是什么?

    下一篇: 错误:无法找到或加载主类