初次搭建spring boot maven项目报错之Unable to find main class

r囧r小猫 2021-09-14 04:32 425阅读 0赞

首次搭建spring boot 项目时pom.xml如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.mycompany</groupId>
  5. <artifactId>SpringBoot</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <packaging>war</packaging>
  8. <name>SpringBoot</name>
  9. <properties>
  10. <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <java.version>1.8</java.version>
  13. </properties>
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>1.3.1.RELEASE</version>
  18. <relativePath/> <!-- lookup parent from repository -->
  19. </parent>
  20. <dependencies>
  21. <!--spring boot-->
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>javax</groupId>
  28. <artifactId>javaee-web-api</artifactId>
  29. <version>7.0</version>
  30. <scope>provided</scope>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-maven-plugin</artifactId>
  38. </plugin>
  39. <plugin>
  40. <groupId>org.apache.maven.plugins</groupId>
  41. <artifactId>maven-compiler-plugin</artifactId>
  42. <version>3.1</version>
  43. <configuration>
  44. <source>1.7</source>
  45. <target>1.7</target>
  46. <compilerArguments>
  47. <endorseddirs>${endorsed.dir}</endorseddirs>
  48. </compilerArguments>
  49. </configuration>
  50. </plugin>
  51. <plugin>
  52. <groupId>org.apache.maven.plugins</groupId>
  53. <artifactId>maven-war-plugin</artifactId>
  54. <version>2.3</version>
  55. <configuration>
  56. <failOnMissingWebXml>false</failOnMissingWebXml>
  57. </configuration>
  58. </plugin>
  59. <plugin>
  60. <groupId>org.apache.maven.plugins</groupId>
  61. <artifactId>maven-dependency-plugin</artifactId>
  62. <version>2.6</version>
  63. <executions>
  64. <execution>
  65. <phase>validate</phase>
  66. <goals>
  67. <goal>copy</goal>
  68. </goals>
  69. <configuration>
  70. <outputDirectory>${endorsed.dir}</outputDirectory>
  71. <silent>true</silent>
  72. <artifactItems>
  73. <artifactItem>
  74. <groupId>javax</groupId>
  75. <artifactId>javaee-endorsed-api</artifactId>
  76. <version>7.0</version>
  77. <type>jar</type>
  78. </artifactItem>
  79. </artifactItems>
  80. </configuration>
  81. </execution>
  82. </executions>
  83. </plugin>
  84. </plugins>
  85. </build>
  86. </project>

构建时报错信息如下

  1. --- spring-boot-maven-plugin:1.3.1.RELEASE:repackage (default) @ SpringBoot ---
  2. ------------------------------------------------------------------------
  3. BUILD FAILURE
  4. ------------------------------------------------------------------------
  5. Total time: 5.188 s
  6. Finished at: 2017-09-05T11:25:05+08:00
  7. Final Memory: 23M/286M
  8. ------------------------------------------------------------------------
  9. Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.1.RELEASE:repackage (default) on project SpringBoot: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.1.RELEASE:repackage failed: Unable to find main class -> [Help 1]
  10. repackage failed: Unable to find main class -> [Help 1]
  11. To see the full stack trace of the errors, re-run Maven with the -e switch.Re-run Maven using the -X switch to enable full debug logging.

其中抓取重要信息

  1. Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.1.RELEASE:repackage (default) on project SpringBoot: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.1.RELEASE:repackage failed: Unable to find main class -> [Help 1]

提取信息为:

  1. repackage failed: Unable to find main class -> [Help 1]

原因找到了,是因为缺少主类,修饰一下就是没有spring boot java入口配置类

于是:新建java文件就可以了:

  1. package com.mycompany.springboot;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. /**
  4. * TODO(在这里用一句话描述这个类的作用)
  5. * @ClassNmae:NewClass
  6. * @author zlx-雄雄
  7. * @date 2017-9-5 11:29:44
  8. *
  9. */
  10. @SpringBootApplication
  11. public class NewClass {
  12. public static void main(String[] args) {
  13. }
  14. }

我这里用的开发工具是Netbeans8.0.3,但是这个配置跟开发工具没有关系
其他开发工具可以参考:http://blog.csdn.net/Evan\_Leung/article/details/52498301?locationNum=7&fps=1

发表评论

表情:
评论列表 (有 0 条评论,425人围观)

还没有评论,来说两句吧...

相关阅读