Access a resource outside a jar from the jar
Asked Answered
M

2

12

I'm trying to access a resource from a jar file. The resource is located in the same directory where is the jar.

my-dir:
 tester.jar
 test.jpg

I tried different things including the following, but every time the input stream is null:

[1]

String path = new File(".").getAbsolutePath();
InputStream inputStream = this.getClass().getResourceAsStream(path.replace("\\.", "\\") + "test.jpg");

[2]

File f = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
InputStream inputStream = this.getClass().getResourceAsStream(f.getParent() + "test.jpg");

Can you give me some hints? Thanks.

Marcasite answered 22/8, 2016 at 13:44 Comment(6)
getResourceAsStream gets a resource from the jar. docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.htmlMclain
"getResourceXXX" will load resources from the classpath, what you need is to open the jpg as a file or add my-dir to the classpath.Dispassion
Have you tried splitting up the code, so you are at least certain that you obtain the correct path to the .jpg file?Refine
@LarsKristensen - I did print the path to the .jpg and it seems correct with option [1]Marcasite
@Mclain - Does this mean that I should use another approach to get the resource ?Marcasite
this might help.Fantasia
S
11

If you are sure, that your application's current folder is the folder of the jar, you can simply call InputStream f = new FileInputStream("test.jpg");

The getResource methods will load stuff using the classloader, not through filesystem. This is why your approach (1) failed.

If the folder containing your *.jar and image file is in the classpath, you can get the image resource as if it was on the default-package:

class.getClass().getResourceAsStream("/test.jpg");

Beware: The image is now loaded in the classloader, and as long as the application runs, the image is not unloaded and served from memory if you load it again.

If the path containing the jar file is not given in the classpath, your approach to get the jarfile path is good. But then simply access the file directly through the URI, by opening a stream on it:

URL u = this.getClass().getProtectionDomain().getCodeSource().getLocation();
// u2 is the url derived from the codesource location
InputStream s = u2.openStream();
Stone answered 22/8, 2016 at 14:8 Comment(0)
G
0

Use this tutorial to help you create a URL to a single file in a jar file.

Here's an example:

String jarPath = "/home/user/myJar.jar";
String urlStr = "jar:file://" + jarPath + "!/test.jpg";
InputStream is = null;
try {
    URL url = new URL(urlStr);
    is = url.openStream();
    Image image = ImageIO.read(is);
}
catch(Exception e) {
    e.printStackTrace();
}
finally {
    try {
        is.close();
    } catch(Exception IGNORE) {}
}
Ganymede answered 22/8, 2016 at 14:33 Comment(1)
OP clearly says jar and jpg are in same folder. He is not asking how to access a resource INSIDE jar.Dinh

© 2022 - 2024 — McMap. All rights reserved.