8

I am trying to build a simple SpringBoot application. When I run my spring boot application it shutdown immediate after starting, below is the console log:

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v1.4.1.BUILD-SNAPSHOT)

2016-09-06 18:02:35.152  INFO 22216 --- [           main] com.example.SpringBootDemo1Application   : Starting SpringBootDemo1Application on IN-FMCN882 with PID 22216 (E:\workspace\springBoot\SpringBootDemo1\target\classes started by Rahul.Tyagi in E:\workspace\springBoot\SpringBootDemo1)
2016-09-06 18:02:35.158  INFO 22216 --- [           main] com.example.SpringBootDemo1Application   : No active profile set, falling back to default profiles: default
2016-09-06 18:02:35.244  INFO 22216 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@14dd9eb7: startup date [Tue Sep 06 18:02:35 IST 2016]; root of context hierarchy
2016-09-06 18:02:36.527  INFO 22216 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-09-06 18:02:36.546  INFO 22216 --- [           main] com.example.SpringBootDemo1Application   : Started SpringBootDemo1Application in 1.781 seconds (JVM running for 2.376)
2016-09-06 18:02:36.548  INFO 22216 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@14dd9eb7: startup date [Tue Sep 06 18:02:35 IST 2016]; root of context hierarchy
2016-09-06 18:02:36.550  INFO 22216 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

Below is my code:

SpringBootDemo1Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Configuration
@EnableAutoConfiguration
@ComponentScan
@Controller
public class SpringBootDemo1Application {


    @ResponseBody
    @RequestMapping("/")
    public String entry(){
        return "My spring boot application";
    }

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

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>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.BUILD-SNAPSHOT</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

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

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

I want to keep the server running so that client can hit it for response. Please Suggest.

RaT
  • 1,134
  • 3
  • 12
  • 28
  • Did you try with a proper release version? 1.4.0.RELEASE for the spring boot starter? – ameenhere Sep 07 '16 at 07:31
  • 1
    Can you also try executing the command "mvn dependency:tree" and verify if the tomcat dependency exists in the project. – ameenhere Sep 07 '16 at 07:38
  • how did you run the application? – kuhajeyan Sep 07 '16 at 12:17
  • @kuhajeyan I am using eclipse STS, in which i run my project as "Spring Boot App" – RaT Sep 07 '16 at 12:48
  • install maven, and run your application mvn spring-boot:run. and see if get the same error. your setup looks ok. but better you separate you controller class from main boot application – kuhajeyan Sep 07 '16 at 13:03
  • [Resolution: the app is not a webapp because it doesn't have an embedded container (e.g. Tomcat) on the classpath. Adding one fixed it. If you are using Maven, then add this in pom.xml:](https://stackoverflow.com/a/22409655/5153843) – Erycoking Oct 02 '17 at 21:27

10 Answers10

9

The only possible explanation i can think of is that tomcat embedded jar is not included in the dependencies/jar. Since you have already defined "spring-boot-starter-web" dependency, it should have transitively pulled the embedded tomcat dependencies as well. But somehow it is excluded.

Things to try out.

  1. Execute "mvn dependency:tree" and check whether the tomcat dependencies exist and in "compile" scope
  2. Change the spring boot starter version to 1.4.0.RELEASE.
ameenhere
  • 2,203
  • 21
  • 36
  • tomcat dependencies are there, before and after executing "mvn dependency:tree" command – RaT Sep 07 '16 at 12:41
  • After changing spring boot starter version to 1.4.0.RELEASE, it starts showing compilation error "can not resolve class ComponentScan, Component, Configuration". – RaT Sep 07 '16 at 12:47
  • Execute "mvn clean install" so that maven downloads the required jars for the new release. If the error still persists, can you paste the import statements in your class as well. – ameenhere Sep 07 '16 at 12:57
  • Not that it matters, but you can remove (Configuration,ComponentScan,Configuration) and add only SpringBootApplication. Its the same, as mentioned [in the doc](http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html) – ameenhere Sep 07 '16 at 13:03
  • After "mvn clean install" the problem persist. I have updated my import statements in code above. When i use SpringBootApplication annotation then it still show error in controller annotation and "The type org.springframework.context.ConfigurableApplicationContext cannot be resolved." error at SpringApplication.run(SpringBootDemo1Application.class, args). – RaT Sep 08 '16 at 06:12
  • 1
    What was the output of mvn clean install? Was the build successful or Build Failure? If it was successful, you have your jar created in /target/ directory. You can then execute "mvn spring-boot:run" to start your spring boot app. – ameenhere Sep 08 '16 at 06:46
  • I got it running. As i am using 1.4.1.BUILD-SNAPSHOT version so i need to add repositories in my pom. Now i understand why did you suggest the RELEASE version. – RaT Sep 09 '16 at 12:20
  • I guess if i were not getting compilation error with 1.4.0.RELEASE, it would have also been working. thanks @CodeItLikeAmeen – RaT Sep 09 '16 at 12:21
3

It worked when I changed spring boot version as follows:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
  </parent> 
Enayat
  • 3,904
  • 1
  • 33
  • 47
3

Try adding server.port=someAvailablePortNumber in the application.properties file located in "resources" folder.

I was also facing same problem. tried a lot of changes suggested in pom.xml file but nothing worked for me. In my case the port 8080 wasn't available so it application wasn't able to start tomcat using default port(i.e.: 8080) causing it to shutdown immediately.

Changing the port number helped to start tomcat and application started working. Hope it helps :)

Maria
  • 123
  • 8
1

Clearing my local Maven repo solved this issue for me. Easiest way is to remove ~/.m2/repository

KC Baltz
  • 1,498
  • 1
  • 13
  • 22
1

removed spring-boot-starter-tomcat dependency and it worked.

0

If we are not using a .RELEASE version of spring we need to add below repositories in our pom.xml file.

And we can use command "mvn spring-boot:run" to run our spring application from command line.

<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
RaT
  • 1,134
  • 3
  • 12
  • 28
0

What solved it for me was updating the "parent" reference in the pom.xml.

this is my working pom.xml :

<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>com.boot</groupId>
<artifactId>project-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

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

<name>project-boot</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

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

Dror
  • 5,107
  • 3
  • 27
  • 45
0

In your main function "SpringApplication.run(Main.class, args.close());" should not be in close , it shuld be like "SpringApplication.run(Main.class, args);"

Example :

@SpringBootApplication
public class Main{
  public static void main(String[] args) {
    SpringApplication.run(Main.class, args);
  }
}
0

Check your log config, maybe you're trying to save logs into the folder, that can't be created.

<appender name="allFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/var/log/app/all.log</file>
         ...
</appender>
arctica
  • 336
  • 4
  • 14
0

i encountered the same problem, my os is win7, my ide is eclipse, after i changed spring boot version to 1.5.7.release, erverything is ok.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>

i have tried spring boot 2.0.2.release, 1.5.13.release, 1.5.1.release, they all cant run on my win7 os using eclipse, but they can run my ubuntu16.04 using command line and mvn compile with the same code.

y119777
  • 1
  • 1