12

I am trying out a simple spring boot application it always shuts down automatically

 :: Spring Boot ::        (v1.4.1.RELEASE)
2016-10-23 13:05:21.681  INFO 16532 --- [           main] com.example.RestBootApplication          : No active profile set, falling back to default profiles: default
2016-10-23 13:05:21.766  INFO 16532 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6e20b53a: startup date [Sun Oct 23 13:05:21 EDT 2016]; root of context hierarchy
2016-10-23 13:05:23.682  INFO 16532 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-10-23 13:05:23.704  INFO 16532 --- [           main] com.example.RestBootApplication          : Started RestBootApplication in 2.632 seconds (JVM running for 5.168)
2016-10-23 13:05:23.705  INFO 16532 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6e20b53a: startup date [Sun Oct 23 13:05:21 EDT 2016]; root of context hierarchy
2016-10-23 13:05:23.708  INFO 16532 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

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>

    <name>rest-boot</name>
    <description>Demo project for Spring Boot</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

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


</project>

Main Class

@SpringBootApplication
public class RestBootApplication {

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

Controller

@Controller
public class HelloController {

    @RequestMapping("/hello")
    String helloWorld(){
        return "helloWorld";
    }

}

Trying to run in spring tool suite. it always stops after starting. I even added "spring-boot-starter-web" after looking at some stackoverflow questions, but still facing the issue.

Please can you someone point out the issue.

Umar
  • 1,002
  • 3
  • 12
  • 29
  • try to remove this dependency < -spring-boot-starter- > from your pom – AchillesVan Oct 23 '16 at 17:44
  • Have you refreshed your Eclipse buildpath etc. after adding `spring-boot-starter-web`? If not, then maybe your app is still running without it, which means after it is started, no other threads (like webserver) run, and thus the application context is immediately shutdown. – dunni Oct 23 '16 at 18:01
  • Possible duplicate of [Spring boot application shutdown immediate after starting](http://stackoverflow.com/questions/39363570/spring-boot-application-shutdown-immediate-after-starting) – Michael Lihs Oct 23 '16 at 22:00

16 Answers16

31

In my case just adding following to pom file

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
jdev
  • 5,304
  • 1
  • 17
  • 20
  • 3
    ` org.springframework.boot spring-boot-starter-web ` The spring-boot-starter-web dependency is starter for building web apps, including RESTful applications, using Spring MVC. It uses Tomcat web server as the default embedded container by including spring-boot-starter-tomcat, but you can use spring-boot-starter-jetty or spring-boot-starter-undertow instead which are starter for using Jetty and Undertow as the embedded servlet container. – Mahesh Vemula Feb 27 '21 at 06:39
17

This was happening to me and it turned out to be a corrupted maven apache repository.

To fix it I removed the apache repo - on my Mac it was located at /Users/myname/.m2/repository/org/apache.

On a pc it should be c:\users\myname.m2\repository\org\apache.

I then ran Maven - Update Project and then I ran my class and it was fixed.

Darkman
  • 223
  • 1
  • 3
  • 14
  • 3
    I was stuck with this problem and with many a stackoverflow question about this exact problem, and nobody had pointed out this stuff before. I guess this is why we don't like multiple questions about the same thing. – Haakon Løtveit Aug 08 '17 at 12:13
  • Thanks.It solved for me. But I was wondering how did you find the problem/solution? – Shrey Shivam Mar 25 '18 at 20:29
  • I had triple checked the configuration so I knew everything was setup properly but it still wasn't working. So I figured something must have been corrupted when Maven was doing it's work so I removed the repo it and let Maven try again - et Viola! – Darkman Mar 27 '18 at 17:10
7

Check what exit code you are getting.

If you get "Process finished with exit code 1" means an exception is being thrown. Thus you can put a try catch block around your SpringApplication.run() statement and print out the stack trace.

public static void main(String[] args) {
    try {
        SpringApplication.run(MyApplication.class, args);
    } catch (Exception e) {
        e.printStackTrace(); 
    }
}

Example talen from: https://stackoverflow.com/a/59017774/9531109

Frankie
  • 24,627
  • 10
  • 79
  • 121
JaredCS
  • 427
  • 4
  • 11
2

Solution to your Question

  1. I reviewed your POM.xml, it looks fine and doesn't require any changes to it.
  2. spring-boot-starter-web is an opinionated dependency, it automatically pulls your other dependencies. Please check whether it has pulled your embedded Tomcat in your Maven Dependencies as shown below.

Dep

  1. Go to application.properties file or application.yml to change the server port server.port=9081and run the maven goal as mvn clean install spring-boot:run -e


    If these options doesn't workout please try this approach posted by me.
Community
  • 1
  • 1
Praveen Kumar K S
  • 3,024
  • 1
  • 24
  • 31
2

First of all check if the port 8080 is available or is being used by some other process.

If this port is not available, 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 and also tried multiple suggestion related to maven (e.g: deleting folder, updating project etc) but nothing worked for me. In my case the port 8080 wasn't available so 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 it started working. Hope it helps :)

Maria
  • 123
  • 8
2

For me the embed tomcat was corrupted. I have done a mvn build and found below line as a WARNING.

[WARNING] error reading /home/syam/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.23/tomcat-embed-core-8.5.23.jar; invalid LOC header (bad signature)

So deleted the tomcat embed directory and did a maven clean, things started working.

Biscuit Coder
  • 477
  • 4
  • 12
1

My suggestion is to remove this dependency

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

You may also try to add this tomcat dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <version>1.4.1.RELEASE</version>
</dependency>
AchillesVan
  • 4,156
  • 3
  • 35
  • 47
  • 2
    `spring-boot-starter-web` includes `spring-boot-starter`, so while it's not necessary to declare it separately, it doesn't harm and doesn't cause the problems. – dunni Oct 23 '16 at 17:59
  • Do you see this tomcat-embed-core jar file in your maven dependncies folder ? or check the mvn log when you execute package phase ,and make sure that the tomcat-embed-core jar is assigned into you app jar. if you do then your jar might be corrupted. Reload it – AchillesVan Oct 23 '16 at 18:13
  • i deleted the application and switch to spring boot version 1.3.8.RELEASE and the application was running. When i switch to 1.4.1 it didn't work – Umar Oct 23 '16 at 19:36
  • Same here, version 1.5.2.RELEASE stops immediately, 1.3.8.RELEASE works – Andrea May 19 '17 at 12:34
1

I had the same problem and there is how I resolved it :

  • I deleted maven's local repository
  • run again maven install
  • run the app as spring boot app

====> Hallelujah, and it worked just by magic !

hlayachi
  • 31
  • 3
  • +1 I had the same problem on my Eclipse 2018-09 on Mac OS X. I tried installing lots of eclipse plugins. Doing a maven repository cleanup did the trick. And I don't know why it not works as well. – MichaelHuelsen Oct 21 '18 at 20:04
0

I just took your code and started a Spring-Boot application from scratch. On my machine, your code starts up as expected when I run

mvn spring-boot:run

To make sure, we have the very same code, I uploaded my solution to Github, see https://github.com/michaellihs/stackoverflow-40205600

Just an idea why it doesn't work for you: do have another Tomcat instance running that is listening on port 8080 - this will stop your application immediately (but normally shows a different log / error message).

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
0

In my case as well , I had to clear the complete .m2 directory and run clean install to make it to work. Not sure what is conflicting.

AK47
  • 39
  • 5
0

i changed the port from 8080 to 8083 in application.properties which is under resources folder. server.port=8083 and it worked.

Lakshmi K
  • 11
  • 1
0

Adding this for posterity.

For these kind of issues it is very advantageous to run mvn help:effective-pom and look at the versions that have been picked up. It is common to have a parent module that is overriding a module/version that spring-boot is expecting to be available.

Chris Hinshaw
  • 6,967
  • 2
  • 39
  • 65
0

I had the same problem and there is how I resolved it : I changed version in pom.xml from 1.4.2.RELEASE to 1.4.1.RELEASE

Ryan Schaefer
  • 3,047
  • 1
  • 26
  • 46
0

If you have created the project using start.spring.io you might have missed to add 'Spring Web' dependency.

alternate we can add below code to pom.xml

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Binod Singh
  • 682
  • 4
  • 11
  • 22
0

This property spring.main.web-application-type= none from the application properties file was causing the application to shut down as soon as started. Removing this would do the job.

Parthiban
  • 43
  • 1
  • 11
-1

I deleted the maven dependencies folder and it worked for me