"Error: The main class could not be found or could not be loaded" when compiling and running Java programs on the command line

Source: Internet
Author: User

Beginner Java, today try compiling and running Hello.java in the command line interface

(File current directory ..... \src\chapter1)

1  PackageChapter1;2 3  Public classHello {4      Public Static voidMain (string[] args) {5         if(args.length==0)6System.out.println ("hello!");7         Else{8              for(inti=0;i<args.length;i++)9 System.out.println (Args[i]);Ten         } One     } A}

You encounter an error prompt:

Error: The main class Hello is not found or cannot be loaded

This issue was addressed through access to information.

Cause of the problem:

The concept of package in Java is not clear. This procedure is the previous editing test in Eclipse, direct copy to use, so that the program segment left redundant package declaration

1  Package chapter1;

The Hello class puts it under the Chapter1 package, while the environment variable classpath sets the current directory, the current directory is ... src \ Chapter1 , the actuator will find chapter1 this package in the current directory, but actually there is no chapter1 under. So the class Hello is not found.

Workaround:

1. Remove the package statement

2, in the current directory and then create a chapter1 folder, put the source files in. (... \src\chapter1\chapter1)/runtime fallback to the previous level directory (...\src)

Note that compiling is sure to be in the path of the file (...\src\chapter1), otherwise there will be an error.

same error other possible scenarios: Check that the environment variables are correct

"Error: The main class could not be found or could not be loaded" when compiling and running Java programs on the command line

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.