Java 运行 spring boot jar 时找不到或加载主类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43597408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:04:07  来源:igfitidea点击:

Could not find or load main class, when running spring boot jar

javaspringmavenspring-boot

提问by J. Zhang

I Have issues running a jar that was created through 'mvn package'. I tried various solutions with no success.

我在运行通过“mvn 包”创建的 jar 时遇到问题。我尝试了各种解决方案,但没有成功。

pom.xml

pom.xml

<groupId>org.springframework</groupId>
<artifactId>rest-service</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.4.RELEASE</version>
</parent>
...
<properties>
  <java.version>1.8</java.version>
  <start-class>ves.sfdc.Application</start-class>
</properties>


<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.0.0</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

projectroot\src\main\java\ves\sfdc\application.java

projectroot\src\main\java\ves\sfdc\application.java

@SpringBootApplication
@Configuration
@ComponentScan
@EnableAsync
@EnableScheduling
@EnableAutoConfiguration

public class Application{

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    AccountService accountService;
    @Autowired
    static
    SfdcUtil sfdcUtil= new SfdcUtil();
    @Autowired
    NamedParameterJdbcTemplate jdbcTemplate2;    

    public static void main(String[] args) throws SecurityException, IOException {   
        SpringApplication.run(Application.class, args);
    }        
}

This project works fine in Eclipse, and when I do mvn spring-boot:run

这个项目在 Eclipse 中运行良好,当我执行mvn spring-boot:run 时

I Wonder if I'm missing something here?

我想知道我是否在这里遗漏了什么?

回答by Amit Gujarathi

The problem in this case is with maven . The dependencies you have downloaded not having the required version. Some dependencies work in bulk with each other and some times they only match up with particular version of other jars.

在这种情况下,问题出在 maven 上。您下载的依赖项没有所需的版本。一些依赖项彼此批量工作,有时它们只与其他 jar 的特定版本匹配。

Solution:- It will take some time but clear .m2 and rebuild maven to download all dependencies again.
or 
you are having two version of same dependencies.
So check all maven jars and remove jars having common names.
Solution:- It will take some time but clear .m2 and rebuild maven to download all dependencies again.
or 
you are having two version of same dependencies.
So check all maven jars and remove jars having common names.

回答by dunni

With Spring Boot you don't need the maven-shade-plugin. Spring Boot will take care of the necessary packaging.

使用 Spring Boot,您不需要 maven-shade-plugin。Spring Boot 将负责必要的包装。

If you have multiple classes with main methods, you can configure the spring-boot-maven-plugin with the correct one:

如果您有多个具有 main 方法的类,您可以使用正确的一个来配置 spring-boot-maven-plugin:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <mainClass>ves.sfdc.Application</mainClass>
  </configuration>
</plugin>

You can find a list of possible configurations in the documentation of the maven plugin: http://docs.spring.io/spring-boot/docs/1.5.3.RELEASE/maven-plugin/repackage-mojo.html

您可以在 maven 插件的文档中找到可能的配置列表:http: //docs.spring.io/spring-boot/docs/1.5.3.RELEASE/maven-plugin/repackage-mojo.html

回答by SatyaRajC

In my case it is the incorrect/improper dependency. Found in markers > problemsis highlighting .pomis not valid archive.

在我的情况下,这是不正确/不正确的依赖。在标记中找到> 问题是突出显示 .pom无效存档。

compile group: 'org.apache.httpcomponents', name: 'httpcomponents-client', version: '4.5.6', ext: 'pom'

compile group: 'org.apache.httpcomponents', name: 'httpcomponents-client', version: '4.5.6', ext: 'pom'

Changed this to

将此更改为

    `compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6'`