How to start Spring Quick Boot Start Code

I'm new to Spring and Spring Boot and I've tried this. I am having a problem running the code from https://projects.spring.io/spring-boot/ .

package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

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

      

I have released mvn install

and everything seems fine. But then I java -cp target/myArtifId-1.0-SNAPSHOT.jar hello.SampleController

threw a ClassNotFoundException too.

How do I run this sample code?

+3


source to share


2 answers


According to spring boot documentation , you can start your application with this command:

java -jar target/myArtifId-1.0-SNAPSHOT.jar

      



spring Boot creates executable jar, no need to specify java class with method main

. This is also the reason why you might not want to include another class with a method main

.

+2


source


I prefer to use the run

Spring Boot Maven Plugin target to compile and run it in one command:



mvn spring-boot:run

      

+1


source







All Articles