Home >Java >javaTutorial >How to solve the problem that the Java package does not exist when importing the package
There are many solutions on the Internet. I have summarized all the ones I thought of for future reference. You may be able to solve the problem by using one of them. I used the last one to solve the problem.
If the package you want to import does not exist under Libraries, then you have to check whether there are any problems with the corresponding dependencies in the pom, and whether there are any names, versions, etc. in the warehouse you are using. If there is no problem with the corresponding jar package, then try the first few methods below to solve it.
Function: Re-import the Maven package
2 ways:
1. Right-click on the Project directory-> Maven -> reload project
2. Maven plug-in refresh (recommended)
Function: clear the Idea cache and restart Idea .
In many cases, problems caused by ideas can basically be solved through this method.
Method: -> Toolbar -> File -> Invalidate Caches / Restart
Function: Set the encoding to UTF-8.
Method: Set all to UTF-8, don’t forget to click Apply after setting
There are many ways:
1. Click rebuild under the Build menu
2. Click 1,2 under the corresponding project/module under the maven plug-in. install represents reinstalling the package.
When the error as shown in the title appears, don’t worry. This is because the Java program package is configured. There is an error in the block. At the same time, you may not have set up IDEA to automatically load the Jar package, so this error will be reported. The solution is as follows:
File—>Setting—>Build, Execution, Deployment—>Maven—>Importing
ps: The problem I had was not solved by the above methods, but was solved by the next method. Hope you don't need to see this. 8. If none of the above methods can solve the problem, it is undoubtedly a problem with IDEA. If you are using the 2020.1~3 version, the probability of having a problem is greater.
Solution: Replace IDEA If the build/run action is delegated to maven
However, if you feel that the startup is slow, you need to check whether it has been compiled twice. You can write a test class and insert a piece of data as soon as the compilation starts to see if there are two items in the database; you can also directly check The compilation box under idea.
Why is it compiled twice?
After the above entrust to maven is checked, the problem of not finding the package is solved, but the maven plug-in and the idea may be compiled one after another.
Solution: Replace the compilation plug-in in pom.xml with
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin>Then you find that it will only be compiled once. There will be no problem if you start the project locally through idea.
However, when you package the project and start it through jar mode, an error may occur and the main attribute cannot be found. Of course, there is no better. If there is, just continue to add the following code.
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
The above is the detailed content of How to solve the problem that the Java package does not exist when importing the package. For more information, please follow other related articles on the PHP Chinese website!