Solve the "weird" error in Eclipse: The main class cannot be found or loaded.

Source: Internet
Author: User

Solve the "weird" error in Eclipse: The main class cannot be found or loaded.

I found many articles, which helped me solve my current problems.

Connection: https://blog.csdn.net/u010512607/article/details/46350813

In the last two days, when I used Eclipse to write code, all the generated. if you delete all the class files, you can re-generate the new class file as long as you re-run the file. If any problem occurs, the console prompts-> error: the main class cannot be found or loaded ....... I have been busy for a long time and there is no effective solution. Later, I found it online and finally found the real cause.

Originally, the Java compiler in Eclipse was based on the new and old versions of the. java file to determine whether to compile. That is to say, if a. java file is not updated, the compiler in Eclipse will not re-compile it.

This is the main reason why I click "RUN error" after deleting all. class files. After the. class file is deleted, it will not be re-compiled when the. java file remains unchanged. Therefore, the. class file cannot be found when the JVM runs the. class file, so an error is reported. So how can we solve this problem?

We know that by default, Eclipse will prompt the following when you click the "Run" icon for the first time or right-click "Run As" a source file:

Figure 1

That is, let us choose whether to automatically save the source file before running, so that after the source file is modified, it can be immediately reflected in the running results. If Always save resources before launching is checked, the system checks whether the file has been updated and saved. If you want to set the file, you can set the parameters in Window> Preferences> Run/Debug> Launching:

Figure 2

Note that in Eclipse, if you select to save the source file before running, the source file will also be compiled, because the default Windows-> Preferences-> General-> Workspace contains the following:

Figure 3

As long as the source file changes (after saving), it will automatically compile. The disadvantage is that you must do something and save it. If the source file is not used in other files. when you customize a class in a java file, you can make symbolic modifications to it to make it new, and then run it. Otherwise, you have to perform the same operation on the classes used and save and run them.

Note: however, this is too troublesome, and it is not realistic for a project that contains a large number of source files. Therefore, we need to find a solution that can be solved once and for all. The answer is:

Project-> Clean...

Clean... the operation clears all previous compilation information of the project, and by default, all. java files are compiled once, so that you can easily modify and run them.

After writing so much, I would like to summarize several solutions to the error reported when deleting the. class file:

1. For a single file that does not use a custom class in another. java file, you can perform symbolic modifications to it, add spaces or similar operations, and save the changes. If other classes are used, you must modify and save them in the corresponding class, or save them after modification.

2. Simple, crude, and effective method: Project-> Clean... clears the previous compilation information of the Project and recompiles it all.

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.