【问题标题】:Could not find main class Spring-Boot application找不到主类 Spring-Boot 应用程序
【发布时间】:2019-05-06 07:41:18
【问题描述】:

我无法在 intellij 中运行我的 Spring-Boot Framework 应用程序。我正在使用 maven,由于某种原因它找不到我的主类。同样在我的 Spring 应用程序类中,我收到一个 SpringApplication.run 错误,显示无法访问 org.springframework.core.env.EnvironmentCapable。

我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<artifactId>SeleniumPoker</artifactId>
<version>1.0-SNAPSHOT</version>


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>

<dependencies>
    <!-- spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-messaging</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--Utility -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.3.2</version>
    </dependency>
    <dependency>
        <groupId>com.intellij</groupId>
        <artifactId>annotations</artifactId>
        <version>9.0.4</version>
    </dependency>
    <!-- Testing -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.47.1</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>2.47.1</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>2.47.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.4</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.4</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-spring</artifactId>
        <version>1.2.4</version>
        <scope>test</scope>
    </dependency>
    <!-- Logging -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>
    <!--  JSON -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20090211</version>
    </dependency>
</dependencies>


<properties>
    <java.version>1.8</java.version>
    <start-class>ca.carleton.poker.PokerApplication</start-class>
</properties>

<build>
    <resources>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
        </resource>
        <resource>
            <directory>${basedir}/src/main/java</directory>
        </resource>
    </resources>

    <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>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                    <descriptorRef>code</descriptorRef>
                </descriptorRefs>
                <finalName>${pom.artifactId}</finalName>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <testFailureIgnore>true</testFailureIgnore>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<groupId>SeleniumPoker</groupId>

这是我的 StarterClass:

package ca.carleton.poker;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import ca.carleton.poker.game.PokerSocketHandler;

/**
 * Main class - launch the application and register endpoint handlers.
 *
 * 
  */
@SuppressWarnings("SpringFacetCodeInspection")
@Configuration
@EnableAutoConfiguration
@EnableWebSocket
@ComponentScan(basePackages = "ca.carleton.poker")
public class PokerApplication extends SpringBootServletInitializer 
implements WebSocketConfigurer {

@Autowired
private PokerSocketHandler PokerSocketHandler;

public static void main(final String[] args) {
    SpringApplication.run(PokerApplication.class, args);
}

@Override
public void registerWebSocketHandlers(final WebSocketHandlerRegistry 
webSocketHandlerRegistry) {
    webSocketHandlerRegistry.addHandler(this.PokerSocketHandler, "/game")
            .withSockJS();
}

@Override
protected SpringApplicationBuilder configure(final SpringApplicationBuilder springApplicationBuilder) {
    return springApplicationBuilder.sources(PokerApplication.class);
}

}

【问题讨论】:

  • 你需要使用这个特定的版本吗?也许您可以尝试最新的,看看是否可行:2.1.1.RELEASE
  • 那么,如何从 Idea 运行它?换句话说,您的运行配置是什么?另外,您是否尝试过简单地使用mvn spring-boot:run从终端执行它?
  • 您显然将其打包为 WAR,但您的 POM 文件中没有 &lt;package&gt; 元素。因此,首先将其包含在内,以便 Maven 清楚您的意图。

标签: java maven spring-boot intellij-idea


【解决方案1】:

看起来版本与 spring 核心库和 springboot 版本不匹配,尝试兼容版本,如果仍然发现问题,请完全删除 .m2 文件夹并再次运行查看,祝你好运。

【讨论】:

  • 我如何知道哪些版本兼容?
猜你喜欢
  • 2015-04-11
  • 2019-05-04
  • 2018-01-30
  • 2019-04-28
  • 2022-01-21
  • 2019-05-16
  • 2018-01-15
  • 1970-01-01
  • 2016-01-10
相关资源
最近更新 更多