加载 Spring Boot 应用程序内的第三方 JAR 包时出现问题。

huangapple 未分类评论45阅读模式
英文:

Problem with loading a third party jar inside a spring boot application

问题

我正在创建一个Spring Boot应用程序,出于业务原因,我需要导入一个第三方jar包,该包还从名为application.properties的文件中读取一些配置参数。

我已经在Spring Boot的application.properties文件中定义了我应用程序和第三方jar包所需的所有配置。

当我从Eclipse运行此应用程序时,一切正常,但当我尝试使用命令从外部运行它时,jar包内部的代码找不到application.properties文件并抛出异常;

java -jar myapp.jar

当我将命令更改为以下命令,并将application.properties文件放在jar包外部时,它可以正常工作;

java -Xbootclasspath/a: -jar myapp.jar

如何在不将application.properties文件放在jar包外部的情况下使其正常工作?

一些额外的信息;
第三方jar是作为我的Maven依赖项包含的库jar。在第三方代码内部,有一个地方它正在加载application.properties文件,但是会抛出异常。

    Properties cfg = new Properties();
	FileInputStream is = null;
	
	URL url = DemoApplication.class.getResource("/");
	if (null != url) {
		
		String configPath = url.getFile() + "application.properties";
		try {
			is = new FileInputStream(configPath);
			cfg.load(is); // 加载失败在这里
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

异常:

file:/D:/Temp/target/myapp.jar!/BOOT-INF/classes!/application.properties 加载配置失败 java.io.FileNotFoundException: file:\D:\Temp\target\myapp.jar!\BOOT-INF\classes!\application.properties(文件名、目录名或卷标语法不正确)
英文:

I am creating a Spring Boot application, for a business reason I need to import a 3rd party jar which also reads some config parameter from a file named application.properties.

I have defined all the configs required by my app and the 3rd party jar in the spring boot application.properties file.

When I run this application from Eclipse all is well, but when I run it from outside by using command the code inside the jar can't find the application.properties file and throws exception;

java -jar myapp.jar

when I change it to following command and by placing the application.properties file outside the jar it works;

java -Xbootclasspath/a: -jar myapp.jar

How can make it work without placing the application.properties file from outside the jar?

Some additional Info;
3rd party jar is a lib jar included as my maven dependency. Inside the 3rd Party code there is a place where it's loading application.properties file, but throws exception.

    Properties cfg = new Properties();
	FileInputStream is = null;
	
	URL url = DemoApplication.class.getResource("/");
	if (null != url) {
		
		String configPath = url.getFile()+ "application.properties";
		try {
			is = new FileInputStream(configPath);
			cfg.load(is); // It fails here
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

Exception:

file:/D:/Temp/target/myapp.jar!/BOOT-INF/classes!/application.properties FAILED load config.java.io.FileNotFoundException: file:\D:\Temp\target\myapp.jar!\BOOT-INF\classes!\application.properties (The filename, directory name, or volume label syntax is incorrect) 

答案1

得分: 0

如果这是一个Maven项目,请尝试使用(mvn package)命令,然后在目标文件夹下运行生成的jar文件。

英文:

If it's a maven project, try to use (mvn package) command and then run the jar generated under the target folder

答案2

得分: 0

如果你的第三方应用是我App的Maven依赖,你可以从类路径中加载它:

Properties cfg = new Properties();
cfg.load(DemoApplication.class.getClassLoader().getResourceAsStream("application.properties"));
英文:

If your thirdparty-app is a maven dependency of myApp you can load it from the classpath:

Properties cfg = new Properties();
cfg.load(DemoApplication.class.getClassLoader().getResourceAsStream("application.properties"));

答案3

得分: 0

第三方的 JAR 包使用了一个 FileInputStream,其 URL 是从 Class.getResource(...) 中获取的。

这在处理 JAR 文件时是行不通的。

可以尝试的方法是跳过 FileInputStream,而使用 Class.getResourceAsStream(...):

InputStream is = DemoApplication.class.getResourceAsStream("/application.properties");
英文:

That 3rd Party jar uses a FileInputStream with a URL that starts with something derived from Class.getResource(...).

That's never going to work with jar files.

What can work is to skip the FileInputStream, but use Class.getResourceAsStream(...):

InputStream is = DemoApplication.class.getResourceAsStream("/application.properties")

huangapple
  • 本文由 发表于 2020年4月8日 20:03:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/61100230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定