简体   繁体   English

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

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

I am trying to package a Java spring boot (Maven) project into JAR file.我正在尝试将 package 一个 Java spring 引导(Maven)项目到 Z529E634C8C25A170CF 文件中So that I can take that JAR file into another computer and simply run it.这样我就可以将该 JAR 文件放入另一台计算机并简单地运行它。 The file is created inside "target" folder.该文件是在“目标”文件夹中创建的。 I can run the project fine with following:我可以通过以下方式正常运行项目:

java -jar target/project.jar

but whenever I take the Jar file to another place (like, into another PC) and try to run like this:但是每当我将 Jar 文件带到另一个地方(例如,进入另一台 PC)并尝试像这样运行时:

java -jar project.jar

it's showing White Label Error 404 page at - localhost:8080它显示白色 Label 错误 404 页面- localhost:8080

How to package the project to run as standalone JAR file without any such errors?如何将 package 项目作为独立的 JAR 文件运行而不会出现任何此类错误?

Here's my application.properties file content:这是我的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


And this is my pom.xml content:这是我的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>


Any suggestion for me? 对我有什么建议吗?
Note: Please don't suggest WAR packaging. 注意:请不要建议使用 WAR 包装。 I am trying to distribute the project as JAR package. 我正在尝试将项目分发为 JAR package。

spring.datasource.url=jdbc:mariadb://localhost:3306/sample Your application has a dependency in some local database. spring.datasource.url=jdbc:mariadb://localhost:3306/sample您的应用程序在某些本地数据库中有依赖关系。

It can be that in your computer the database exists and is reachable by the application so the application can start and play correctly.可能是您的计算机中存在数据库并且应用程序可以访问该数据库,因此应用程序可以正确启动和播放。

On other computers the database may not exist or may not be reachable from the application so Spring context fails during initialization and therefore the web application is not reachable from you!在其他计算机上,数据库可能不存在或无法从应用程序访问,因此 Spring 上下文在初始化期间失败,因此您无法访问 web 应用程序!

If as pointed in comments this is not the issue, then there can be another issue as well.如果如评论中所指出的,这不是问题,那么也可能存在另一个问题。

You can replace the following in your pom.xml您可以在pom.xml中替换以下内容

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

with

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

So that the final Jar is a repackaged Fat jar which will contain also all runtime dependencies needed.因此,最终的 Jar 是重新打包的 Fat jar,其中还包含所需的所有运行时依赖项。

I get such error in the past You are having this error because there is no defaut webPage for spring boot Screenshot with the error我过去收到此类错误您遇到此错误是因为 spring 引导没有默认网页

To fix it, you just need to add a simple html file ( index.html ) in you src/main/resources/statics directory要修复它,您只需在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. 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. This is because of the jar packaging,that the jsp files are not getting copied from the WEB-INF folder.这是因为 jar 包装,jsp 文件没有从 WEB-INF 文件夹中复制。 If you keep the jsp files under the META-INF/resources folder while using jar as packaging it should work.如果您将 jsp 文件保存在 META-INF/resources 文件夹下,同时使用 jar 作为包装,它应该可以工作。

Related Question: Why does Spring boot not support jsp while it can render the page if we add proper jar reference相关问题: 为什么 Spring 启动不支持 jsp 而如果我们添加正确的 jar 引用它可以呈现页面

You might have to configure a view resolver in this case.在这种情况下,您可能必须配置视图解析器。 Typically we place JSP files in the ${project.basedir}/main/webapp/WEB-INF/jsp/.通常我们将 JSP 文件放在 ${project.basedir}/main/webapp/WEB-INF/jsp/ 中。 In such a case, you will have to add following 2 configuration parameters in application.properties.在这种情况下,您必须在 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