无法运行Spring Boot测试应用程序

时间:2018-11-30 11:36:18

标签: java maven spring-boot intellij-idea

尝试完成Spring Boot教程:https://spring.io/guides/gs/spring-boot/#initial

这是我的课程:

package hello;

import java.util.Arrays;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

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

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {

            System.out.println("Let's inspect the beans provided by Spring Boot:");

            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }

        };
    }

}

package hello;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }

}

和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>

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

当我尝试使用以下命令运行它:“ mvn软件包&& java -jar target / gs-spring-boot-0.1.0.jar”时,我得到了:

  

“ C:\ Program Files \ Java \ jdk1.8.0_181 \ bin \ java.exe”

     

-Dmaven.multiModuleProjectDirectory = C:\ Users \Антон\ IdeaProjects \ SpringBootTest

     

“-Dmaven.home = C:\ Program Files \ JetBrains \ IntelliJ IDEA   2018.2.3 \ plugins \ maven \ lib \ maven3“

     

“-Dclassworlds.conf = C:\ Program Files \ JetBrains \ IntelliJ IDEA   2018.2.3 \ plugins \ maven \ lib \ maven3 \ bin \ m2.conf“

     

“-javaagent:C:\ Program Files \ JetBrains \ IntelliJ IDEA   2018.2.3 \ lib \ idea_rt.jar = 63804:C:\ Program Files \ JetBrains \ IntelliJ IDEA 2018.2.3 \ bin“
  -Dfile.encoding = UTF-8 -classpath“ C:\ Program Files \ JetBrains \ IntelliJ IDEA   2018.2.3 \ plugins \ maven \ lib \ maven3 \ boot \ plexus-classworlds-2.5.2.jar“ org.codehaus.classworlds.Launcher

     

-Didea.version = 2018.2.3软件包&& java

     

-jar target / spring-boot-test-0.1.0.jar

     

无法解析命令行选项:无法识别的选项:-jar

我试图从Windows控制台和“ Maven项目”->“执行Maven目标”执行它。

找不到有关此错误的任何信息。

2 个答案:

答案 0 :(得分:1)

打开命令提示符,然后转到pom.xml所在的位置 运行 mvn clean install 一旦您获得成功的消息构建

按cd目标转到目标文件夹 然后运行命令java -jar <file-name>.jar

或转到项目文件夹文件夹并直接运行:

mvn spring-boot:run

如果在运行时仍收到错误消息,则说明某些spring配置错误。

https://start.spring.io/创建新的Spring Boot应用程序

答案 1 :(得分:1)

您可以尝试从项目的根文件夹运行mv spring-boot:run

或者,运行mvn clean install,清理不是默认目标,因此最好将清理目标添加到清理目标文件夹。