简体   繁体   English

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

[英]Can't run Spring Boot test application

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

Here are my classes: 这是我的课程:

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!";
    }

}

and pom.xml: 和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>

When I am trying to run it with this command: "mvn package && java -jar target/gs-spring-boot-0.1.0.jar", I am getting this: 当我尝试使用以下命令运行它时:“ mvn package && java -jar target / gs-spring-boot-0.1.0.jar”,我得到以下信息:

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

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

"-Dmaven.home=C:\\Program Files\\JetBrains\\IntelliJ IDEA 2018.2.3\\plugins\\maven\\lib\\maven3" “ -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" “ -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" “ -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 -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 。启动器

-Didea.version=2018.2.3 package && java -Didea.version = 2018.2.3包&& java

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

Unable to parse command line options: Unrecognized option: -jar 无法解析命令行选项:无法识别的选项:-jar

I tried to execute it from windows console and from "Maven Projects" -> "Execute Maven Goal". 我试图从Windows控制台和“ Maven项目”->“执行Maven目标”执行它。

Can't find anything about this error. 找不到有关此错误的任何信息。

Open command prompt and goto your where pom.xml presents run mvn clean install once you get the message build successful 打开命令提示符,并在成功构建消息后转到pom.xml所在的位置运行mvn clean install

goto target folder by cd target then run the command java -jar <file-name>.jar 通过cd target转到goto目标文件夹,然后运行命令java -jar <file-name>.jar

or goto project folder folder and directly run : 或转到项目文件夹文件夹并直接运行:

mvn spring-boot:run

if you are still getting the error message while running then some spring config is wrong. 如果您在运行时仍然收到错误消息,则说明某些spring配置错误。

create new spring boot application from https://start.spring.io/ https://start.spring.io/创建新的Spring Boot应用程序

You may can try to run mv spring-boot:run , from the root folder of your project. 您可以尝试从项目的根文件夹运行mv spring-boot:run

Or, run mvn clean install , clean is not a goal for default, so is good add clean goal to clean target folder. 或者,运行mvn clean install ,clean不是默认目标,因此最好将clean目标添加到目标文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM