Using Java Advanced Imaging with Maven

JavaMaven 2Jai

Java Problem Overview


The JAI setup is quite tedious, involving multiple jars and environment variables. It would aid the project's portability quite a lot if I could add it as a regular Maven dependency.

The POM snippet I'm using is

<dependency>
  <groupId>com.sun.media</groupId>
  <artifactId>jai_imageio</artifactId>
  <version>1.1</version>
</dependency>

and the errors are

[INFO] ------------------------------------------------------------------------                               
[ERROR] BUILD ERROR                                                                                             
[INFO] ------------------------------------------------------------------------                                 
[INFO] Failed to resolve artifact.                                                                              

Missing:   
----------
1) com.sun.media:jai_imageio:jar:1.1
2) javax.media:jai_core:jar:1.1.3

I can, of course, download and install those jars. The problem is twofold:

  • jai_imageio requires two jars;
  • jai_imageio requires a native library to be installed and two environment variables to be set.

I have not found a way to make this work with Maven.


See Reading JCS_YCCK images using ImageIO for the reason I'm using JAI.

Java Solutions


Solution 1 - Java

To avoid donwloading the jars and installing them you can add a dependency on the spring repo. So change the normal dependency slightly:

    <dependency>
        <groupId>javax.media.jai</groupId>
        <artifactId>com.springsource.javax.media.jai.core</artifactId>
        <version>1.1.3</version>
    </dependency>

and add a repository declaration:

    <repository>
        <id>com.springsource.repository.bundles.external</id>
        <name>SpringSource Enterprise Bundle Repository - External Bundle Releases</name>
        <url>http://repository.springsource.com/maven/bundles/external</url>
    </repository>

And it should now work (it makes all the sun classes available javax.media.jai.*). See here:

http://ebr.springsource.com/repository/app/bundle/version/detail?name=com.springsource.javax.media.jai.core&version=1.1.3

You can also add the codec dependency if necessary...

http://ebr.springsource.com/repository/app/bundle/version/detail?name=com.springsource.javax.media.jai.codec&version=1.1.3

Solution 2 - Java

There is a "standalone" implementation of JAI-imageio, without dependencies to jai_core. It doesn't need JAI installation to your JDK and JRE, only single Maven dependency.

In Maven, add it's repository:

<repository>
	<releases />
	<snapshots>
		<enabled>false</enabled>
	</snapshots>
	<id>mygrid-repository</id>
	<name>myGrid Repository</name>
	<url>http://www.mygrid.org.uk/maven/repository</url>
</repository>

and dependency:

<dependency>
	<groupId>net.java.dev.jai-imageio</groupId>
	<artifactId>jai-imageio-core-standalone</artifactId>
	<version>1.2-pre-dr-b04-2014-09-13</version>
</dependency>

See its site for more details

PS Updated after a useful comment (another dependency from gitHub which does not need adding that repository):

<dependency>
    <groupId>com.github.jai-imageio</groupId>
    <artifactId>jai-imageio-core</artifactId>
    <version>1.3.0</version>
</dependency>

Solution 3 - Java

You're going to need to download the jars and install them in your local maven repository, or local repository proxy server (Nexus/Artifactory). I think you can use the maven-enforcer-plugin to validate that the environment settings are there.

The licence for jai_imageio doesn't allow for it to be distributed.

Solution 4 - Java

This worked for me:

<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-coverage</artifactId>
    <version>2.7.4</version>
</dependency>

<repository>
    <id>osgeo</id>
    <name>Open Source Geospatial Foundation Repository</name>
    <url>http://download.osgeo.org/webdav/geotools/</url>
</repository>

It seems that gt-coverage depends on jai_imageio, so it installed the appropriate jars for me. I didn't even have to change my code to use this artifact.

This will get your code working within your IDE. However, if you want an executable jar, then you need to use the Maven Shade plugin. Its use is described here and here. Note the extra lines in the 2nd link because they are necessary. Here's the extra code to go in your pom:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.3.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <manifestEntries>
                            <Main-Class>com.companyname.packagename.MainClassName</Main-Class>
                            <Specification-Title>Java Advanced Imaging Image I/O Tools</Specification-Title>
                            <Specification-Version>1.1</Specification-Version>
                            <Specification-Vendor>Sun Microsystems, Inc.</Specification-Vendor>
                            <Implementation-Title>com.sun.media.imageio</Implementation-Title>
                            <Implementation-Version>1.1</Implementation-Version>
                            <Implementation-Vendor>Sun Microsystems, Inc.</Implementation-Vendor>
                            <Extension-Name>com.sun.media.imageio</Extension-Name>
                        </manifestEntries>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

I don't know what all those extra manifest entries are, but they make my executable jar do what it does in the IDE.

Solution 5 - Java

The repository url in manunu's answer seems to have changed or at least is temporarily unavailable, which causes the maven build to fail. As an alternative, the following url can be used:

http://build.mygrid.org.uk/maven/repository

<repository>
   <releases />
   <snapshots>
      <enabled>false</enabled>
   </snapshots>
   <id>mygrid-repository</id>
   <name>myGrid Repository</name>
   <url>http://build.mygrid.org.uk/maven/repository</url>
</repository>

Solution 6 - Java

<dependency>
    <groupId>javax.media.jai</groupId>
    <artifactId>com.springsource.javax.media.jai.core</artifactId>
    <version>1.1.3</version>
</dependency>

and add a repository declaration:

<repository>
    <id>com.springsource.repository.bundles.external</id>
    <name>SpringSource Enterprise Bundle Repository - External Bundle Releases</name>
    <url>http://repository.springsource.com/maven/bundles/external</url>
</repository>

This worked for me. I guess it relies on Spring Jar.

Solution 7 - Java

What I failed to see was that the JAI dependency needed to be satisfied only at runtime, and therefore I made sure the production environment has access to JAI, by configuring it for Tomcat.

Solution 8 - Java

try this:

<dependency>
  <groupId>com.sun.media</groupId>
  <artifactId>jai_imageio</artifactId>
  <version>1.1</version>
  <type>pom</type>
</dependency>

Solution 9 - Java

I add this dependencies in my pom file:

<dependency>
		<groupId>com.github.jai-imageio</groupId>
		<artifactId>jai-imageio-core</artifactId>
		<version>1.4.0</version>
</dependency>	

from https://openmeetings.apache.org/openmeetings-web/dependencies.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobert MunteanuView Question on Stackoverflow
Solution 1 - JavaLolView Answer on Stackoverflow
Solution 2 - JavamanunaView Answer on Stackoverflow
Solution 3 - JavaMike CornellView Answer on Stackoverflow
Solution 4 - JavaGregNashView Answer on Stackoverflow
Solution 5 - JavaChris2MView Answer on Stackoverflow
Solution 6 - JavaSwarit AgarwalView Answer on Stackoverflow
Solution 7 - JavaRobert MunteanuView Answer on Stackoverflow
Solution 8 - JavaI-yorn-manView Answer on Stackoverflow
Solution 9 - Javaedilon JuniorView Answer on Stackoverflow