Spring boot 打包后 jar文件,IDEA调试时候没什么问题,可是打包成执行jar后发现有异常抛出:
class path resource [1.jpg] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/opt/app.jar!/BOOT-INF/classes!/1.jpg
经实践可用的方法:
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
Resource resource = new ClassPathResource("1.jpg");
//InputStream inputStream = resource.getInputStream();
Image image = tool.getImage(resource.getURL());
调试、打包都是正常的
spring boot 打包后 jar文件,IDEA调试时候没什么问题,可打包成执行jar后发现有异常抛出:class path resource [1.jpg] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/opt/app.jar!/BOOT-INF/classes!/1.jpg经实践可用的方法: import org.springframe
在项目有读取配置
文件
的地方,项目
打包
为
jar
后,无法从外部替换默认的配置
文件
,通过
Spring
Boot
一系列的配置可以
解决
该问题。
直接谷歌搜索查到:
Spring
Boot
Executable
Jar
with
Classpath
其中一个 Peter Tarlos 的答案是完整的,本文的内容也是以这里为起点,通过查找官方文档来说明如何实现。
关键的 PropertiesLauncher
Executable
Jar
s
Spring
Boot
’s executable
jar
s, their
一、java -
jar
eureka-0.0.1-SNAPSHOT.
jar
为什么可以直接运行?
解压eureka-0.0.1-SNAPSHOT.
jar
META-INF下的
MANIFEST
.MF
文件
内容为:
Manifest
-Version: 1.0
Implementation-Title: eureka
Implementation-Version: 0.0.1-SNAPSHOT
Archiv...
项目需要另一个子项目Utils的一个util工具类,在A项目的maven中加入了该子项目
<dependency>
<groupId>com.supconit.data.algorithm.platform</groupId>
<artifactId>data_algorithm_util</artifactId>
1、group.json为
classpath
目录下的
文件
,FileCopyUtils为
spring
boot
框架自带工具
Resource resource = new
ClassPath
Resource(“group.json”);
byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream());
String con...
1、可
执行
jar
目录结构
在使用
spring
-
boot
-maven-plugin插件
执行
mvn package命令构建可
执行
jar
文件
(Fat
JAR
)后用“java -
jar
”命令就可以直接运行应用程序。
<plugin>
<groupId>org.
spring
framework.
boot
</groupId>
<artifactId>sprin...
在将
Spring
Boot
项目打
成
jar
包后,如果工具类无法读取模板
文件
,可能是因为模板
文件
没有被正确地
打包
进
jar
包中。这可以通过以下方法
解决
:
1. 确认模板
文件
的路径和位置。在
Spring
Boot
项目中,模板
文件
通常位于/resources/templates目录下。确保
文件
名和相对路径都正确。
2. 使用Maven插件来
打包
项目。例如,在pom.xml
文件
中添加以下配置:
<build>
<plugins>
<plugin>
<groupId>org.
spring
framework.
boot
</groupId>
<artifactId>
spring
-
boot
-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
3. 将模板
文件
的位置和路径添加到ResourceLoader中。例如,可以在工具类中使用以下代码来
获取
模板
文件
:
ResourceLoader resourceLoader = new FileSystemResourceLoader();
Resource resource = resourceLoader.getResource("
classpath
:/templates/template.html");
其中,
classpath
:/templates/template.html是模板
文件
的相对路径。
4. 如果仍然无法读取模板
文件
,可以尝试将模板
文件
打包
到
jar
包的根目录下。在pom.xml
文件
中添加以下配置:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
这将在
打包
时将src/main/resources目录下的所有
文件
打包
到根目录下。
综上所述,将
Spring
Boot
项目
打包
成
jar
包后,如果工具类无法读取模板
文件
,需要注意模板
文件
的路径和位置,并使用Maven插件来
打包
项目。此外,还可以将模板
文件
打包
到
jar
包的根目录下。
IFRAME 嵌套报Refused to display ‘http://...‘ in a frame because it set ‘X-Frame-Options‘ to ‘deny‘.解决方式
wdy234:
Mysql “Too many connections“ 异常
CSDN-Ada助手:
RabbitMQ Windows安装与启动服务
Bsciology:
@AllArgsConstructor作用
会魔法的小:
@AllArgsConstructor作用
会魔法的小: