简体   繁体   中英

java, loading of resources fails: unable to resolve file:/my-jar.jar!/folder/my-file

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

I copy all the resources in the jar file.

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.

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

Specifically, it throws an IOException like this

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

How should I load the resources?

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

EDIT:

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");

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

if you have an structure like this:

src/main/Begin.java
src/main/folder/icon.png

Try this in the Begin.java:

  Begin.getClass().getResource("/main/folder/icon.png");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM