Unable to find a single main class from the following candidates


1、Unable to find a single main class from the following candidates

1.1、問題描述

maven build時出現以下錯誤提示日志:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE:repackage (default) on project information: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.hhly.InformationApplication, com.hhly.test.Application] -> [Help 1]

1.2、日志分析

Unable to find a single main class from the following candidates [com.hhly.InformationApplication, com.hhly.test.Application] // 不能從下面的候選類中找到單一的main類

 

1.3、解決辦法

查看着兩個類,發現兩個類中確實兩個類中均有一個main方法,去掉一個多余的main方法,保留唯一的main方法。

2、jar中沒有主清單屬性

2.1、問題描述

生產對應的jar包之后,通過一下命令運行spring boot程序,

java -jar information-0.0.1-SNAPSHOT.jar
  • 1
  • 1jar中沒有主清單屬性

2.2、問題分析

查找資料發現為最后生成的jar包中的META-INF/MANIFEST.MF文件,沒有設置主函數信息。猜想是pom.xml設置的問題,比對網上的設置,發現多數配置都是如下:

    <build> <plugins> <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> <configuration> <maimClass>com.hhly.InformationApplication</maimClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

比對自己的設置發現:自己在標簽外面還包了一個 pluginManagement標簽。

2.3、解決辦法

去掉pluginManagement標簽。

3、Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required

3.1、問題描述

首先通過maven clean,然后再執行maven build,在執行main函數時會出現下面錯誤,詳細日志如下:

2016-09-09 18:29:43.419 WARN 37076 --- [ost-startStop-1] o.s.b.f.s.DefaultListableBeanFactory : Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMapper' defined in file [D:\neon-workspace\information \target\classes\com\hhly\dao\UserMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

3.2、問題分析

同樣的代碼,在通過Alt + F5更新項目,然后maven build生成jar包,最后執行的main的時候也就不會報錯。

3.3、解決辦法

調整打包順序如下: 
1、Alt + F5 
2、maven build

spring-boot-maven-plugin 插件的作用

   POM 文件中添加了“org.springframework.boot:spring-boot-maven-plugin”插件。在添加了該插件之后,當運行“mvn package”進行打包時,會打包成一個可以直接運行的 JAR 文件,使用“Java -jar”命令就可以直接運行。這在很大程度上簡化了應用的部署,只需要安裝了 JRE 就可以運行。

可以在POM中,指定生成 的是Jar還是War。

<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">
<!-- ... -->
<packaging>jar</packaging>
<!-- ... -->
</project>

 

你還可以指定要執行的類,如果不指定的話,Spring會找有這個【public static void main(String[] args)】方法的類,當做可執行的類。

如果你想指定的話,可以用下面兩個方法:

1,如果你的POM是繼承spring-boot-starter-parent的話,只需要下面的指定就行。

<properties> <!-- The main class to start by executing java -jar --> <start-class>com.mycorp.starter.HelloWorldApplication</start-class> </properties>
2,如果你的POM不是繼承 spring-boot-starter-parent的話,需要下面的指定。

 

 

    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>1.3.5.RELEASE</version>
      <configuration>
        <mainClass>${start-class}</mainClass>
        <layout>ZIP</layout>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>repackage</goal>
          </goals>
        </execution>
      </executions>
    </plugin>

 

from:

http://docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html
http://stackoverflow.com/questions/23217002/how-do-i-tell-spring-boot-which-main-class-to-use-for-the-executable-jar
http://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html
http://udn.yyuap.com/doc/Spring-Boot-Reference-Guide/III.%20Using%20Spring%20Boot/13.1.4.%20Using%20the%20Spring%20Boot%20Maven%20plugin.html
http://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/#listing1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM