简体   繁体   English

spring boot 应用程序适用于 intellij 但不适用于 .jar 文件

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

I am new to Spring boot and java.我是 Spring Boot 和 Java 的新手。 I have a simple Spring boot app which has one function.我有一个简单的 Spring Boot 应用程序,它具有一个功能。 When I run it on intellij it works well, and I am able to test it with postman many times without issue.当我在 intellij 上运行它时,它运行良好,我可以用邮递员多次测试它而不会出现问题。 But when I build an artifact and then .jar file, running that .jar file will close the program immediately.但是当我构建一个工件然后构建 .jar 文件时,运行该 .jar 文件将立即关闭程序。 there is no error or exception, after starting it closes.没有错误或异常,启动后关闭。 Also I tried to add tomcat manually and test it but does not help.我也尝试手动添加tomcat并对其进行测试,但没有帮助。
I checked few solutions like this link which mention about tomcat dependency, but my pom.xml file has no tomcat even inside .我检查了几个解决方案,比如这个链接,其中提到了 tomcat 依赖项,但是我的 pom.xml 文件甚至在里面也没有 tomcat

This the command line I run the .jar file with java -cp demo.jar CICD.demo.CicDtestApplication这是我使用java -cp demo.jar CICD.demo.CicDtestApplication运行 .jar 文件的命令行

EDITED:编辑:
as mentioned in the comment, i tried mvn clean package command and then in target directory there is the .jar file.如评论中所述,我尝试了mvn clean package命令,然后在目标目录中有 .jar 文件。 so then running java -jar demo.jar CICD.demo.CicDtestApplication works nice.所以然后运行 ​​java -jar demo.jar CICD.demo.CicDtestApplication 效果很好。

    <?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.如果您有任何想法,请告诉我。

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).但我猜大部分时间会发生这种情况(您的清单文件可能不好,因为您没有正确构建 Spring Boot 应用程序)。

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.正确构建应用程序的一种可能解决方案是使用 spring boot mvn 插件,但正如我所见,您已经添加了这个 maven 插件,因此您只需要使用适当的参数执行它。

Try to create the jar with this command: mvn clean package尝试使用以下命令创建 jar: mvn clean package

If it does not work, please copy the error here...如果它不起作用,请在此处复制错误...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM