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

查看:150
本文介绍了在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:


引起:java.lang.IllegalArgumentException:URI不是分层的
at java.io.File。(Unknown Source)

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天全站免登陆