TwelveMonkeys ImageIO is not working after build in to jar with OpenJDK8

1.7k views Asked by At

I'm currently adding additional functionality to my program by using this library: https://haraldk.github.io/TwelveMonkeys/ TwelveMonkeys ImageIO.

It works great in Editor - Intellij IDEA 2020.1.1 but when I build the project into the jar it's not working.

Error:

Exception in thread "main" javax.imageio.IIOException: Unsupported Image Type
        at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1036)
        at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1007)
        at javax.imageio.ImageIO.read(ImageIO.java:1462)
        at javax.imageio.ImageIO.read(ImageIO.java:1309)

This error is telling that internal java imageio used, not from TwelveMonkeys. I have tried to set priority in modules but that also didn't helped:

Modules Artifacts

Java version:

java -version
openjdk version "1.8.0_252"
OpenJDK Runtime Environment (Zulu 8.46.0.19-CA-win64) (build 1.8.0_252-b14)
2

There are 2 answers

1
AstroCool On BEST ANSWER

@haraldK Thanks for answer. It took me several days to actually figure out but now it's working. A little bit of explanation of what I have done:

  1. Transfer to Maven project
  2. Add to pom.xml all dependencies
  3. Add maven-shade-plugin
  4. Used two transformers: ServicesResourceTransformer and ManifestResourceTransformer
0
PlayerHOI On

I've struggled with this too for a while and after applying the solution suggested by AstroCool my JAR file would still not read SVG files despite having the batik-transcoder and imageio-batik dependencies in the POM file.

So for anyone else struggling with this here's a little bit of an explanation on how to apply the maven-shade-plugin and its transformers and which dependencies you will need for this to work.

First of all, I used outdated dependencies since I was using the search function of IntelliJ and for some reason it found only older version of the batik-transcoder and imageio-batik by TwelveMonkeys.

So here's are the dependencies which I currently use in my project's POM file:

        <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
        <artifactId>batik-transcoder</artifactId>
        <version>1.14</version>
    </dependency>
    <dependency>
        <groupId>com.twelvemonkeys.imageio</groupId>
        <artifactId>imageio-batik</artifactId> <!-- svg -->
        <version>3.8.1</version>
    </dependency>
    <dependency>
        <groupId>com.twelvemonkeys.imageio</groupId>
        <artifactId>imageio-tga</artifactId>
        <version>3.8.1</version>
    </dependency>

Note that you can always find the latest versions for these here by using the search function: https://mvnrepository.com/

To use the maven-shade-plugin apply these to your 'plugins' section of the POM file:

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"/>
                </transformers>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Finally, if you have been making many changes and additions it would be best to clean up your project, in IntelliJ you can do this by clicking on File -> Invalidate Chaches

After that click on Build -> Rebuild project and then build your new JAR file. If you still experience problems it may be worth to create a new Artifact (if using intelliJ) after rebuilding the project.