springspring-bootspring-mvc

Why Spring jar application closes after Start?


My spring application works inside intellij with gradle bootRun. When I build the jar file with Task -> Build -> Jar, going to /build/libs, opening powershell and running "java -jar jar-file.jar", then the following console log appears and the application ends.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.2.0)

15:57:24.858 [main] INFO com.hipertrofiabet.ApplicationStart -- Starting ApplicationStart using Java 17.0.7 with PID 2736 (E:\my github repos\hipertrofia bet\static server\build\libs\hipertrofiabet-1.0-SNAPSHOT.jar started by SaluC in E:\my github repos\hipertrofia bet\static server\build\libs)
15:57:24.861 [main] INFO com.hipertrofiabet.ApplicationStart -- No active profile set, falling back to 1 default profile: "default"
15:57:25.081 [main] INFO com.hipertrofiabet.ApplicationStart -- Started ApplicationStart in 0.337 seconds (process running for 0.524)

It has a ApplicationStart and a Controller.

@SpringBootApplication
public class ApplicationStart {

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

}

@Controller
public class StaticController {

    @RequestMapping(path="/", method=RequestMethod.GET)
    public String indexPage() {
        return "index";
    }

}

this is my build.gradle:

    buildscript {
    repositories {
        maven {
            url = uri("https://plugins.gradle.org/m2/")
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:3.2.0")
    }
}

plugins {
    id "java"
    id "application"
    id "org.springframework.boot" version "3.2.0"
}

group "com.hipertrofiabet"
version "1.0-SNAPSHOT"

repositories {
    gradlePluginPortal()
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot:3.1.5")
    implementation("org.springframework.boot:spring-boot-autoconfigure:3.2.0")
    implementation("org.springframework.boot:spring-boot-starter-web:3.1.5")
    implementation("org.springframework.boot:spring-boot-devtools:3.1.5")
    implementation("org.springframework.boot:spring-boot-starter-thymeleaf:3.1.5")
    implementation("org.springframework.boot:spring-boot-starter-parent:3.1.5")
    implementation("org.springframework.boot:spring-boot-starter-tomcat:3.2.0")
    testImplementation("org.springframework.security:spring-security-test:6.2.0")
    implementation("org.springframework.security:spring-security-web:6.2.0")
    implementation("org.springframework.security:spring-security-core:6.2.0")
    implementation("org.springframework.boot:spring-boot-starter-actuator:3.2.0")
//    implementation("org.springframework.boot:spring-boot-starter-data-redis:3.1.5")
//    implementation("org.springframework.boot:spring-boot-starter-validation:3.1.5")
    implementation("org.yaml:snakeyaml:2.2")
    implementation("commons-io:commons-io:2.15.0")
    implementation("org.apache.commons:commons-lang3:3.13.0")
    compileOnly("org.projectlombok:lombok:1.18.30")
    annotationProcessor("org.projectlombok:lombok:1.18.30")
    testCompileOnly("org.projectlombok:lombok:1.18.30")
    testAnnotationProcessor("org.projectlombok:lombok:1.18.30")
    testImplementation(platform("org.junit:junit-bom:5.9.1"))
    testImplementation("org.junit.jupiter:junit-jupiter:5.9.2")
    implementation("org.jetbrains.kotlin:kotlin-script-runtime:1.9.0")
}

jar {

    manifest {
        attributes "Main-Class" : "com.hipertrofiabet.ApplicationStart"
    }

    duplicatesStrategy = "exclude"

    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }

}

this is my application.yml:

server:
  port: 8081
  address: 0.0.0.0
logging:
  level:
    web: INFO

I've been trying to understand what's happening for the last 4 days, I'm learning spring and I definitely have no more ideas on how to solve this problem.


Solution

  • Make sure to add @RestController annotation instead of @Controller, this is because @RestController includes the @ResponseBody annotation which tells the controller that the return object is serialized to JSON and returned as an HTTP Response.

    IMPORTANT

    When generating a jar file with maven or gradle, spring boot generates two jar files, one which only contains the code you wrote and another which contains both the code you wrote (your classes, interfaces etc.) and the dependencies needed to run the application - this is what we call a fat jar. Make sure to use the fat jar to run your application when using java -jar command.

    Run gradlew build or .\gradlew build to generate this jar files.

    In gradle this jar files are located in /build/libs/* directory. The fat jar to use is the one without the *-plain.jar. As I understand you have been running the hipertrofiabet-1.0-SNAPSHOT-plain.jar instead of hipertrofiabet-1.0-SNAPSHOT.jar file.

    Run java -jar hipertrofiabet-1.0-SNAPSHOT.jar command to run your spring boot application.