繁体   English   中英

Java Spring 引导可执行文件 JAR 文件未运行到另一台机器

[英]Java Spring boot executable JAR file not running into another machine

我正在尝试将 package 一个 Java spring 引导(Maven)项目到 Z529E634C8C25A170CF 文件中这样我就可以将该 JAR 文件放入另一台计算机并简单地运行它。 该文件是在“目标”文件夹中创建的。 我可以通过以下方式正常运行项目:

java -jar target/project.jar

但是每当我将 Jar 文件带到另一个地方(例如,进入另一台 PC)并尝试像这样运行时:

java -jar project.jar

它显示白色 Label 错误 404 页面- localhost:8080

如何将 package 项目作为独立的 JAR 文件运行而不会出现任何此类错误?

这是我的application.properties文件内容:

 spring.datasource.url=jdbc:mariadb://localhost:3306/sample spring.datasource.username=root spring.datasource.password=**** spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDB103Dialect spring.jpa.hibernate.ddl-auto=update ## MULTIPART (MultipartProperties) # Enable multipart uploads spring.servlet.multipart.enabled=true # Threshold after which files are written to disk. spring.servlet.multipart.file-size-threshold=2KB # Max file size. spring.servlet.multipart.max-file-size=200MB # Max Request Size spring.servlet.multipart.max-request-size=215MB ## File Storage Properties # All files uploaded through the REST API will be stored in this directory file.upload-dir=/Users/noticepush/notices spring.mvc.view.prefix: /WEB-INF/jsp/ spring.mvc.view.suffix: .jsp


这是我的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 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>2.6.1</version> <relativePath/> <.-- lookup parent from repository --> </parent> <groupId>com.tech</groupId> <artifactId>StressDetection</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>StressDetection</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>


对我有什么建议吗?
注意:请不要建议使用 WAR 包装。 我正在尝试将项目分发为 JAR package。

spring.datasource.url=jdbc:mariadb://localhost:3306/sample您的应用程序在某些本地数据库中有依赖关系。

可能是您的计算机中存在数据库并且应用程序可以访问该数据库,因此应用程序可以正确启动和播放。

在其他计算机上,数据库可能不存在或无法从应用程序访问,因此 Spring 上下文在初始化期间失败,因此您无法访问 web 应用程序!

如果如评论中所指出的,这不是问题,那么也可能存在另一个问题。

您可以在pom.xml中替换以下内容

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

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

因此,最终的 Jar 是重新打包的 Fat jar,其中还包含所需的所有运行时依赖项。

我过去收到此类错误您遇到此错误是因为 spring 引导没有默认网页

要修复它,您只需在src/main/resources/statics directory statics 目录中添加一个简单的 html 文件( index.html

The issue is that when you use java -*.jar to deploy any springboot application, the jsp files will not be present in the embedded tomcat and while trying to serve the request you will get a 404 PAGE NOT FOUND. 这是因为 jar 包装,jsp 文件没有从 WEB-INF 文件夹中复制。 如果您将 jsp 文件保存在 META-INF/resources 文件夹下,同时使用 jar 作为包装,它应该可以工作。

相关问题: 为什么 Spring 启动不支持 jsp 而如果我们添加正确的 jar 引用它可以呈现页面

在这种情况下,您可能必须配置视图解析器。 通常我们将 JSP 文件放在 ${project.basedir}/main/webapp/WEB-INF/jsp/ 中。 在这种情况下,您必须在 application.properties 中添加以下 2 个配置参数。

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

暂无
暂无

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

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