Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
846 views
in Technique[技术] by (71.8m points)

eclipse - 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.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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 .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...