0

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.

user9137963
  • 105
  • 9

1 Answers1

-1

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...

czupe
  • 4,740
  • 7
  • 34
  • 52
  • I executed with command line, so I realized the .jar file does not work. otherwise, how would i know the .jar file does not work? this is my command line: java -cp demo.jar CICD.demo.CicDtestApplication . running with java -jar demo.jar CICD.demo.CicDtestApplication does not work and says manifest does not exist. – user9137963 Jun 22 '22 at 22:18
  • you can run the jar file just by clicking into it, and you said: "there is no error or exception, after starting it closes", now you say the manifest does not exist, which is the same what I said in my comment if you read it carefully: "your manifest file is not good probably" – czupe Jun 22 '22 at 22:21
  • So again, as said in my comment, just use this command for building from the command line: ```mvn clean package``` probably this will work, I think you built it from Intellij and went to build directory and tried to run, that is my guess why the build was not good. – czupe Jun 22 '22 at 22:22
  • your built java file shall be in target dir. – czupe Jun 22 '22 at 22:23
  • I built the jar file using Intellij artifacts and also manifest file exist in project directory. I said with -cp command it starts and there is no error but immediately closes. I will try mvn command as well – user9137963 Jun 22 '22 at 22:26
  • but if you check the manifest file in the created file by inntelij probably will not be good, easier to do this with the mvn spring-boot plugin with the command I copied. – czupe Jun 22 '22 at 22:29
  • 1
    it worked well, with mvn command and java -jar not java -cp. thanks – user9137963 Jun 22 '22 at 22:34
  • 1
    glad to hear it, forgot to mention that -cp is not needed and the class name because it is in the manifest file (you can uncompress it). I am happy we solved this, have a nice rest of the day. – czupe Jun 22 '22 at 22:56
  • This is not a solution. – Plain_Dude_Sleeping_Alone Oct 31 '22 at 14:55
  • This was the solution for the questioner as she/he marked this as approved, as we solved his issue, as its using his mvn dependency to build his project, so it's not really fair to unvote it afterward. For a separate scenario, there shall be a different solution, probably what you have encountered. – czupe Oct 31 '22 at 18:54