spring-boot 应用程序只能使用 spring-boot:run 在分叉时启动 - java -jar 失败
Spring-boot application can only be launched with spring-boot:run when forking - java -jar fails
我有一个 Spring 启动 Web 应用程序,当我直接使用可执行 jar 时无法启动。
我正在使用 Spring Boot 1.2.0.RELEASE、Maven 3.0.5、Java 1.7.0_72。
我需要使用 hp-roman8 字符集 - 以便处理来自某些远程遗留系统的传入请求。为了提供 hp-roman8 字符集,我在 1.5 版中使用 net.freeutils.jcharset。 jcharset 工件安装在我的本地存储库中
然而,当使用 java -jar 启动我的应用程序时,应用程序无法启动,我得到 "java.nio.charset.UnsupportedCharsetException: hp-roman8" 作为原因。
如果我执行 mvn spring-boot:run 也会出现同样的错误,除非我将 spring-boot-maven-plugin 配置为始终 fork。
使用 <fork>true</fork>
spring-boot:run 成功启动应用程序并且 hp-roman8 字符集在类路径上可用。
但是 <fork>true</fork>
对创建的 jar 没有影响,所以我仍然无法使用 java -jar 启动我的应用程序 - 并继续获取 "java.nio.charset.UnsupportedCharsetException: hp-roman8".
jcharset-1.5.jar 正确包含在创建的可执行 jar 文件中,位于路径 "lib/jcharset-1.5.jar" 中其余依赖项的旁边,所以我不太明白为什么它不是启动 jar 时在类路径上可用。
你们有没有看到类似的行为,或者对我可以尝试什么来解决甚至解决这个问题有任何想法?
更新:
我也尝试过更改主类以使用 PropertiesLauncher(使用插件配置中的 <layout>ZIP</layout>
标记)——参见 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging。
之后我将 loader.path 添加到我的 application.properties。即使我指定了 jcharset-1.5.jar 的绝对路径,我仍然得到 UnsupportedCharsetException。
我也试过使用 exploded archive 但还是不行。
您可以使用 Maven 的 shade 插件而不是 Spring Boot 的 Maven 插件。主要区别在于 shade 插件获取项目的所有依赖项并将它们直接打包到 jar 文件中,即它不使用嵌套的 jar。虽然这有一些缺点,但它确实意味着单个 class 加载器用于加载应用程序的所有 classes,因此,JCharset 可用于应用程序 class 加载器。
当您使用 Shade 插件时,您不应使用 Spring Boot 的起始父级。您可能需要导入 Boot 的依赖管理。
您的 pom 看起来像这样:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-sample-jcharset</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-sample-jcharset</name>
<description>Spring Boot sample showing the use of JCharset in an executable jar</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Any additional dependencies, including JCharset -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>sample.jcharset.SampleJCharsetApplication</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>