在 jar 中加载文件 [英] load file within a jar

查看:29
本文介绍了在 jar 中加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 jar 中打包一个配置文件.配置文件在 jar 文件的根目录下.但是我收到以下错误:

I need to package a configuration file within a jar. the configuration file is under the root of the jar file. However I got the following error:

Caused by: java.lang.IllegalArgumentException: URI is not层次化在 java.io.File.(来源不明)

Caused by: java.lang.IllegalArgumentException: URI is not hierarchical at java.io.File.(Unknown Source)

File url = new File(MyClass.class.getClassLoader().getResource("my.conf").toURI());

推荐答案

你应该使用 getResourceAsStream() 代替.如果文件嵌入在您的 JAR 中,则 URI 很可能是 bundle://URI

You should use getResourceAsStream() instead. If the file is embedded in your JAR the URI is most likely bundle:// URI

InputStream is = this.getClass().getResourceAsStream("my.conf");

这篇关于在 jar 中加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆