英文:
TwelveMonkeys ImageIO is not working after build in to jar with OpenJDK8
问题
我目前正在通过使用这个库向我的程序添加额外的功能:
https://haraldk.github.io/TwelveMonkeys/ TwelveMonkeys ImageIO。
在Intellij IDEA 2020.1.1编辑器中效果很好,但是当我将项目构建成jar文件时却不起作用。
错误:
Exception in thread "main" javax.imageio.IIOException: 不支持的图像类型
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)
此错误表明使用了内部的Java ImageIO,而不是来自TwelveMonkeys。我尝试过在模块中设置优先级,但这也没有帮助:
Java版本:
java -version
openjdk version "1.8.0_252"
OpenJDK Runtime Environment (Zulu 8.46.0.19-CA-win64) (build 1.8.0_252-b14)
英文:
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:
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)
答案1
得分: 1
@haraldK 感谢您的回答。我花了几天时间才真正弄明白,但现在它正在运行。我所做的稍微解释一下:
- 转换为 Maven 项目
- 在
pom.xml
中添加所有依赖项 - 添加
maven-shade-plugin
- 使用了两个转换器:
ServicesResourceTransformer
和ManifestResourceTransformer
。
英文:
@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:
- Transfer to Maven project
- Add to
pom.xml
all dependencies - Add
maven-shade-plugin
- Used two transformers:
ServicesResourceTransformer
andManifestResourceTransformer
答案2
得分: 0
以下是翻译好的部分:
我也曾经为此问题苦苦挣扎了一段时间,尽管我已经按照AstroCool建议的解决方案进行了操作,但我的JAR文件仍然无法读取SVG文件,尽管在POM文件中已经添加了batik-transcoder和imageio-batik的依赖。
所以,对于其他任何与此问题挣扎的人,这里有一些关于如何应用maven-shade-plugin以及其转换器的解释,以及您需要哪些依赖项才能使其正常工作的说明。
首先,由于我在使用IntelliJ的搜索功能,由于某种原因,它只找到了TwelveMonkeys提供的batik-transcoder和imageio-batik的旧版本,因此我使用了过时的依赖项。
以下是我当前在项目的POM文件中使用的依赖项:
<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>
请注意,您始终可以使用搜索功能在此处找到这些依赖项的最新版本:https://mvnrepository.com/
要使用maven-shade-plugin,将以下内容添加到POM文件的“plugins”部分:
<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>
最后,如果您进行了许多更改和添加,最好清理一下项目。在IntelliJ中,您可以通过点击“文件”->“无效缓存”来执行此操作。
之后,点击“构建”->“重新构建项目”,然后构建您的新JAR文件。如果您仍然遇到问题,重新构建项目后创建一个新的Artifact(如果使用IntelliJ)可能会有所帮助。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论