Home » Java » maven – Extract image from resources within a java program packaged as a jar file-Exceptionshub

maven – Extract image from resources within a java program packaged as a jar file-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a java program that reads image resources from disk.

This works well while testing with mvn exec:java.

However, when program is packaged with mvn packaged, it throws the following error:

java.nio.file.FileSystemNotFoundException


Project structure

├── java
│   └── com
│       ├── aaa
│       │   ├── bbb
│       │   │   ├── AssemblingTags.java
│       │   │   ├── BatchLifeCycle.java
│       │   │   ├── config
│       │   │   │   ├── AssemblingTagsConfig.java
│       │   │   │   ├── Configuration.java
│       │   │   │   ├── DatabaseConfig.java
│       │   │   │   ├── ExecutionContext.java
│       │   │   │   ├── InjectionTagsConfig.java
│       │   │   │   ├── QualityConfig.java
│       │   │   │   └── TraceabilityContext.java
│       │   │   ├── Database.java
│       │   │   ├── DeviceLifeCycle.java
│       │   │   ├── Devices.java
│       │   │   ├── InjectionTags.java
│       │   │   ├── QRCode.java
│       │   │   ├── Quality.java
│       │   │   ├── Simulator.java
│       │   │   ├── ThreadManager.java
│       │   │   ├── TimeManager.java
│       │   │   └── Utils.java
│       │   └── traceability
│       │       └── Tracer.java
│       └── example
│           ├── AdminAPI.java
│           ├── ConfigAPI.java
│           ├── CORSFilter.java
│           ├── DeviceAPI.java
│           ├── Main.java
│           ├── QrCodeAPI.java
│           ├── ToggleAPI.java
│           └── TracerAPI.java
└── resources
    └── images
        └── strioscopy
            ├── v1-2.png
            ├── v1-3.png
            ├── v2-1.png
            └── v2-2.png

The piece of code that reads the image

Note the prints.

try {
            System.out.println("images/" + type + "/" + name); <-- PRINT 1
            URL res = getClass().getClassLoader().getResource("images/" + type + "/" + name);
            System.out.println(res);                           <-- PRINT 2
            File file = Paths.get(res.toURI()).toFile();
            FileInputStream fis = new FileInputStream(file);
            byte[] data = new byte[(int) file.length()];
            fis.read(data);
            fis.close();
            return Response.ok(data).build();
        } catch (Exception e) {
            e.printStackTrace();
            return Response.serverError().entity(e).build();
        }

When using mvn exec:java, it works and print:

images/strioscopy/v1-3.png 
file:/home/hduser/program/target/classes/images/strioscopy/v1-3.png

When executing with the jar (java -jar myprogram-jar-with-dependencies.jar), it fails and prints:

images/strioscopy/v1-3.png

jar:file:/home/hduser/program/target/myprogram-jar-with-dependencies.jar!/images/strioscopy/v1-3.png

From my understanding, it is not able to read the images from the jar (as it is “jared”). But how to fix this problem ?

Moreover, notice the exclamation mark here: (between chevrons) jar:file:/home/hduser/program/target/myprogram-jar-with-dependencies.jar>>!<</images/strioscopy/v1-3.png

Is it normal ?

How to&Answers:

For whatever reason, it gives expected result with the following code:

try {
            System.out.println("images/" + type + "/" + name);
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("images/" + type + "/" + name);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int nRead;
            byte[] data = new byte[1024];
            while ((nRead = is.read(data,0,data.length)) != -1) {
                baos.write(data, 0, nRead);
            }
            baos.flush();
            byte[] ba = baos.toByteArray();
            return Response.ok(ba).build();
        } catch (Exception e) {
            e.printStackTrace();
            return Response.serverError().entity(e).build();
        }

However, I am not able to explain why. (If someone can bring light on this, welcome !)