spring boot app works on intellij but not with .jar file

I am new to Spring boot and java. I have a simple Spring boot app which has one function. When I run it on intellij it works well, and I am able to test it with postman many times without issue. But when I build an artifact and then .jar file, running that .jar file will close the program immediately. there is no error or exception, after starting it closes. Also I tried to add tomcat manually and test it but does not help.
I checked few solutions like this link which mention about tomcat dependency, but my pom.xml file has no tomcat even inside.

This the command line I run the .jar file with java -cp demo.jar CICD.demo.CicDtestApplication

EDITED:
as mentioned in the comment, i tried mvn clean package command and then in target directory there is the .jar file. so then running java -jar demo.jar CICD.demo.CicDtestApplication works nice.

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>CICD</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>CICDtest</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </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>
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <releases>
            <enabled>false</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>

Please let me know if you have any idea.

>Solution :

Run your application from the command line, then you will see the exception raised and you can copy it here, or you can try to fix it yourself :).

But I would have a guess which happens most of the time (your manifest file is not good probably, because you did not build properly the Spring Boot Application).

One possible solution to build properly your application is to use the spring boot mvn plugin, but as I see you already added this maven plugin, so you just need to execute it with proper parameters.

Try to create the jar with this command:
mvn clean package

If it does not work, please copy the error here…

Leave a Reply