When executing the java file in the Command window, the system prompts that the main class cannot be found or loaded.

Source: Internet
Author: User

When executing the java file in the Command window, the system prompts that the main class cannot be found or loaded.

When executing the java file in the Command window, the system prompts that the main class cannot be found or loaded.

In the past, java code was written on integrated development tools such as Eclipse or IDEA. Therefore, code compilation and testing were performed with one click. The principle is simply as follows, that is, run the javac command. compile java files. class file, and then run the java command. class file, and finally get the results of the program. As for the underlying things, we will not go into it for the time being. If you are interested, you can take a look at the relevant information such as Java virtual machine. All the work done by running the program was handed over to the compiler. Later, I tried to do this manually.The following error is prompted:

Then I went to the Internet to check it and found that many of my colleagues have encountered this problem.

Let's take a look at my test project directory. This makes it clearer.

I directly created two. java files under the folder, A. java and B. java. The javatest function is similar to the package in java. The code of the two files is as follows:

A. java

B. java

Next, compile the java file.

In this way, two. class files are obtained: A. class and B. class.

Finally, execute the B. class file.

As you can see, the error message is: the primary class B cannot be found or loaded.

Then I went to the Internet to check the reason why some colleagues said it should be package. Then I tried it:

The results show the program call results.

Another method is as follows:

All in allExecute the. class file containing the main function in the upper-level directory of the package.

This article is just like this. I hope to help students who encounter such problems a little bit. Thank you!

 

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.