54

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.

Community
  • 1
  • 1
Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278

9 Answers9

44

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

Lol
  • 940
  • 8
  • 6
  • 10
    Maybe this worked in 2011, but now no more, the stuff just isn't there. – Tilman Hausherr Mar 14 '14 at 06:10
  • this doesn't seem to include the required `imageio` – Don Cheadle Apr 01 '15 at 19:44
  • Please note this doesn't work in IDEA, because it doesn't like the fact that package name is javax. So if you get jar file from the link and add it to the project, it will work. – Stepan Yakovenko Oct 03 '16 at 09:26
  • Worked in my IDEA 2020.2 Community.. Just added repository and later dependency that is mentioned above. For me it was just a trouble to get real repository URL. – BlueDev Apr 19 '21 at 16:20
  • @BlueDev can you share the real repository section which you used? – Zoran777 Apr 07 '22 at 11:05
  • This worked for me on 13 June, 2022. I changed nothing. Thank you. In case it matters, I put the repo reference as the first entry in the list of repositories. – Mark Giaconia Jun 13 '22 at 11:12
14

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>
manuna
  • 729
  • 14
  • 37
  • 2
    turns out the `` part may be out of date, but the [GitHub page README has the which doesn't need an extra repo](https://github.com/stain/jai-imageio-core) – Don Cheadle Apr 01 '15 at 20:12
12

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.

Mike Cornell
  • 5,909
  • 4
  • 29
  • 38
  • Thanks for the answer. I was afraid of that, but I guess I need to create my own POMs for it. – Robert Munteanu Jul 31 '09 at 07:59
  • Well the POMs for the files already exists. http://mirrors.ibiblio.org/pub/mirrors/maven2/com/sun/media/jai_imageio/1.1/jai_imageio-1.1.pom It does have the required dependancy of jai_core. The downside is it has that downloadURL in the distribution management. – Mike Cornell Jul 31 '09 at 14:10
  • This is the right answer if you are concerned with deploying your project to Maven Central. MC do not allow relying on third party repositories which some of the other answers here offer. – Leif Gruenwoldt Aug 31 '13 at 15:19
3

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>
Chris2M
  • 183
  • 1
  • 9
  • I want you to know you're the best – Don Cheadle Apr 01 '15 at 19:49
  • although, actually if you look at the Github site from @manunu 's answer https://github.com/stain/jai-imageio-core you see the dependency in the README, which does not require a repo (I tested it) – Don Cheadle Apr 01 '15 at 19:53
3
<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.

Swarit Agarwal
  • 2,520
  • 1
  • 26
  • 33
2

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.

GregNash
  • 1,316
  • 1
  • 16
  • 25
  • 1
    Unless you *need* the geotools library in your project adding it as a dependency as a work-around to get jai-codec seems like a bad solution. – Leif Gruenwoldt Aug 31 '13 at 15:27
1

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.

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
  • 1
    sir, am facing problems at run time. tomcat is unable to locate the class file. I occasionally get the `NoClassDefFoundError` , what is the proper solution i.e. how to configure tomcat so that it always get the class definition ? Am using these 3 files `jai codec 1.1.3 jai core 1.1.3 jai imageio 1.2` – gursahib.singh.sahni Oct 04 '13 at 06:15
1

try this:

<dependency>
  <groupId>com.sun.media</groupId>
  <artifactId>jai_imageio</artifactId>
  <version>1.1</version>
  <type>pom</type>
</dependency>
I-yorn-man
  • 43
  • 2
1

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