- 1, Problem statement
- 2, Solution
1, Problem statement
My project is the spring boot project. In the pom file, the tag of parent is removed and the dependency management tag is used to manage the spring boot dependency.
When running Java -jar guide-member-0.0.1-snapshot.jar on idea, an error without the main list attribute is reported. The complete error is as follows:
MrzhuangdeMacBook-Pro:target mrzhuang$ java -jar gulimall-member-0.0.1-SNAPSHOT.jar gulimall-member-0.0.1-SNAPSHOT.jar There is no main list attribute in
1. Method 1 (the problem is not really solved)
Check the solution on the Internet. There is no master list attribute method
Reminder: this method does not really solve my problem, which may be related to the structure of the project.
1.1 click file - > project structure
1.1.1 click on artifacts - > "+" - > Jar - > from
1.1.2 select the module and main class. Note that /main/java below needs to be removed, click OK, and then click apply.
1.2 click build - > build artifacts in the menu bar
1.2.1 select the jar package you want to generate and click Build
Finally, in the out folder, the desired jar package is successfully generated.
1.2.2 run the jar package:
Although the problem that there is no main list attribute is solved, another error is reported in my report that the main class cannot be found or loaded.
MrzhuangdeMacBook-Pro:gulimall_member_jar mrzhuang$ java -jar gulimall-member.jar error: The main class cannot be found or loaded. com.zhuang.gulimall.member.GulimallMemberApplication
1.3 methods to solve the problem that the main class cannot be found or loaded
Tip: this method varies depending on the structure of the project. Mine is not solved!
1.3.1 in solving the error: the main class cannot be found or loaded, and the data is also consulted. More importantly, the following configuration is added to the mven plug-in of pom file
<configuration> <mainClass>Relative path of main class</mainClass> </configuration>
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.zhuang.gulimall.member.GulimallMemberApplication</mainClass> </configuration> </plugin>
1.3.2 then rebuild artifacts and run the regenerated jar package under the out path again.
Finally, an error was reported that the main class com.zhuang.gulimall.member.GulimallMemberApplication could not be found or loaded.
2. Method 2 (finally solved the problem)
Add the following configuration in maven plug-in of pom file:
<executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions>
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
Note: the repackage (goal) of the spring boot Maven plug-in has the following two functions:
1. Repackage on the basis of the jar package formed by the original Maven package. The newly formed jar package not only contains application class files and configuration files, but also contains the jar package that the application depends on and Springboot startup related classes (loader s, etc.), so as to meet the characteristics of Springboot independent applications;
2. Rename the jar packaged by the original Maven to XXX.jar.original as the original file;
Execute maven clean package on the project, and you can see that in the target file, the.jar package and the original file with.Original suffix are generated.
Run the generated jar package:
You can see the successful operation!!!