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

Categories

0 votes
841 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 Answer

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 .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...