简体   繁体   中英

Can't run Spring Boot test application

Trying to complete Spring Boot tutorial: 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:

<?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:

"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 package && java

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

Unable to parse command line options: Unrecognized option: -jar

I tried to execute it from windows console and from "Maven Projects" -> "Execute Maven Goal".

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

goto target folder by cd target then run the command 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.

create new spring boot application from https://start.spring.io/

You may can try to run mv spring-boot:run , from the root folder of your project.

Or, run mvn clean install , clean is not a goal for default, so is good add clean goal to clean target folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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