java,加载资源失败:无法解析文件:/my-jar.jar!/ folder / my-file [英] java, loading of resources fails: unable to resolve file:/my-jar.jar!/folder/my-file

查看:158
本文介绍了java,加载资源失败:无法解析文件:/my-jar.jar!/ folder / my-file的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我制作的java应用程序中,我构建了相应的jar文件。

From a java application i made, I build the corresponding jar file.

我复制了jar文件中的所有资源。

I copy all the resources in the jar file.

例如,如果在 src / main / resources 中存在以下资源 / folder / my-file ,然后将它复制到具有相同路径的jar中。

For example, if in src/main/resources there exists the following resource /folder/my-file, then it's copied in the jar with the same path.

但是如果我执行jar,则资源加载失败。

But if i execute the jar, the loading of the resources fails.

具体来说,它会抛出这样的IOException

Specifically, it throws an IOException like this


java.io.IOException:无法解析
file:/my-jar.jar!/ folder / my-file作为类路径,文件名或
URL

java.io.IOException: Unable to resolve "file:/my-jar.jar!/folder/my-file" as either class path, filename or URL

我应该如何加载资源?

如果我在eclipse中运行java应用程序,一切正常,即使资源加载正确。

If I run the java app in eclipse, all works fine, even the resources are loaded correctly.

编辑:

我通过以下方式获取资源的路径:

I'm getting the path of the resource via:

 MyClass.class.getResource("/folder/my-file").getPath();

资源的加载是由外部库完成的,我必须指的是路径。

The loading of the resource is made by an external library, what i have to d is just specify the path.

推荐答案

在这种情况下,我会尝试:

In that case, I would try:

YourClass.class.getClassLoader().getResourceAsStream("folder/my-file");

这将为您的资源返回 InputStream 独立于您的执行环境。

Which would return an InputStream to your resource independently of your execution environment .

这篇关于java,加载资源失败:无法解析文件:/my-jar.jar!/ folder / my-file的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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