TwelveMonkeys ImageIO在将其构建为包含OpenJDK 8的jar文件后无法工作。

huangapple go评论76阅读模式
英文:

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:

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)

答案1

得分: 1

@haraldK 感谢您的回答。我花了几天时间才真正弄明白,但现在它正在运行。我所做的稍微解释一下:

  1. 转换为 Maven 项目
  2. pom.xml 中添加所有依赖项
  3. 添加 maven-shade-plugin
  4. 使用了两个转换器:ServicesResourceTransformerManifestResourceTransformer
英文:

@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

答案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:

        &lt;dependency&gt;
        &lt;groupId&gt;org.apache.xmlgraphics&lt;/groupId&gt;
        &lt;artifactId&gt;batik-transcoder&lt;/artifactId&gt;
        &lt;version&gt;1.14&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.twelvemonkeys.imageio&lt;/groupId&gt;
        &lt;artifactId&gt;imageio-batik&lt;/artifactId&gt; &lt;!-- svg --&gt;
        &lt;version&gt;3.8.1&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.twelvemonkeys.imageio&lt;/groupId&gt;
        &lt;artifactId&gt;imageio-tga&lt;/artifactId&gt;
        &lt;version&gt;3.8.1&lt;/version&gt;
    &lt;/dependency&gt;

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:

            &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.2.4&lt;/version&gt;
            &lt;configuration&gt;
                &lt;transformers&gt;
                    &lt;transformer implementation=&quot;org.apache.maven.plugins.shade.resource.ServicesResourceTransformer&quot;/&gt;
                    &lt;transformer implementation=&quot;org.apache.maven.plugins.shade.resource.ManifestResourceTransformer&quot;/&gt;
                &lt;/transformers&gt;
            &lt;/configuration&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                    &lt;phase&gt;package&lt;/phase&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;shade&lt;/goal&gt;
                    &lt;/goals&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
        &lt;/plugin&gt;

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.

huangapple
  • 本文由 发表于 2020年9月21日 14:16:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63987060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定