Home > Java > javaTutorial > body text

How to solve the problem that the Java package does not exist when importing the package

WBOY
Release: 2023-04-19 10:22:02
forward
1553 people have browsed it

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.

1. Execute Maven ->reload project

Function: Re-import the Maven package
2 ways:
1. Right-click on the Project directory-> Maven -> reload project
2. Maven plug-in refresh (recommended)

How to solve the problem that the Java package does not exist when importing the package

2. Execute Invalidate and Restart

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

3. Unified encoding

Function: Set the encoding to UTF-8.
Method: Set all to UTF-8, don’t forget to click Apply after setting

How to solve the problem that the Java package does not exist when importing the package

4. Recompile

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.

How to solve the problem that the Java package does not exist when importing the package

5. Set the idea to automatically load the jar 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

How to solve the problem that the Java package does not exist when importing the package

##6. Delete the local private repository After that, update maven and reload

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

How to solve the problem that the Java package does not exist when importing the package

# and then recompile and run, it will pass normally.

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>
Copy after login

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>
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template