JAVA cannot find symbol

Preface

When using idea to start the project or Maven package, an error message is reported that the symbol cannot be found.

"Symbol not found" errors usually mean that the compiler cannot find a certain class, method, or variable that you are trying to reference.

Everyone's project situation is different. There may be many reasons for this problem. We must start from the problem to solve the problem.

solution

The first type of missing dependency causes

First of all, the idea should be clear. Don’t be afraid when you see mistakes. Find the problem slowly.

1. If the code starts normally before, the error should not be serious. Check carefully to see if the new pom dependency has not been downloaded. In most cases, this error is reported because a certain class or method cannot be found.

If there are a lot of 'java: Error: symbol not found', it means that these locations have a common problem, and they all use the same thing. Just find an error and track it to find the cause of the error.

For example, in this example, an error is reported that the symbol cannot be found, but you can see that there is an error above. The package xxx.xxx does not exist. Then we can check whether it really exists. If it does not exist, then Just add it in pom

The picture shows the pom.xml file. Remember to refresh the pom file after adding it.

After refreshing, you can restart the project for testing.

The second code is updated but still cached

Option 1 Refresh maven first 

clean description clears the contents of the target folder generated by compilation

complie Description This command can compile the code in the src/main/java directory.

package indicates that if the packaged project is a JavaSe project, package it into a jar package

                                      If it is a JavaWeb project, package it into a war package

install packages the project and installs it into the local warehouse. In fact, before executing install, the packaging is performed first, and then the project is installed into the local warehouse.

After execution is complete, rerun the project for testing.

Option 2 Caching causes

You need to clear the compilation tool cache. Click the File->Invalidate caches/Restart option in the idea tool.

Option 3 Deploy multiple microservice projects

If there are multiple microservice projects as follows: Find the project related to the introduction class, deploy the project, and then package it

It is correct that the compiler changed to display dependencies, but in fact there is no such class file anymore and it needs to be republished in the referenced project.

The third way to check whether the project uses jdk correctly

Check the project background, such as whether JDK1.8 must be used to run other versions. The startup will fail, and the packaging error xx symbol cannot be found.

Option 1: Consider whether the jdk version in the Project in the Project Structure settings is correct. Just make sure that all submodules are changed to jdk1.8.

Option 2: Consider whether the version of each submodule in the Java encoding mode in the settings is 1.8, and make sure that all submodules are set correctly.

Option 3: Consider checking whether the jdk version in SKDS in the Project Structure settings is correct, and make sure that version 8 is loaded on the right side and not other JDK versions.

Option 4: Consider whether the jdk version in Modules in the Project Structure settings is correct.

Summarize

"Symbol not found" errors usually mean that the compiler cannot find a certain class, method, or variable that you are trying to reference. This may be due to several reasons:

  1. Spelling errors : You might have accidentally spelled a name incorrectly, or it has incorrect capitalization.
  2. Missing import : If a symbol (class, method, or variable) is not in the import list of the current file, you cannot reference it directly.
  3. Path issues : You may not have the library or package in the correct path, or you may not have the correct library path set in the project structure.
  4. Version conflict : You may be using different versions of libraries, resulting in conflicts between methods or classes with the same names but different implementations.

To resolve this issue, you can try the following steps:

  1. Check spelling : Make sure you spell the name of a class, method, or variable correctly.
  2. Missing import : If the symbol is not in the import list of the current file, you need to add the correct import statement. For example, if you want to use java.util.ArrayListclasses, you need to add import java.util.ArrayList;.
  3. Check paths : Make sure the library or package is placed under the correct path and that the correct library path is set in the project structure.
  4. Check for version conflicts : If you are using different versions of the library, try to unify the versions, or make sure the names of methods or classes you are using match your library version.
  5. Rebuild the project : Sometimes, cleaning and rebuilding the project can solve unknown problems.
  6. Use IDE help : If you are using IDE (such as IntelliJ IDEA, Eclipse, etc.), they usually have code auto-completion and error prompt functions that can help you find and solve problems.

Keep in mind that solving "symbol not found" problems may require some patience and trial and error. Once you find the cause of the problem, fixing it is relatively simple.

Guess you like

Origin blog.csdn.net/qq_39535439/article/details/134525974