Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
782 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...