1

Using batik-rasterizer.jar from version 1.7 of batik, I was wondering how to call the jar correctly.

java -jar batik-rasterizer-1.7.jar -m image/png -q 0.8 $1

Which gave me:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/batik/i18n/LocalizableSupport

Therefore, I was looking for the correct MainClass and ClassPath to use. I'll post a script I came up with as an answer in a minute ...

Jay
  • 4,627
  • 1
  • 21
  • 30
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186

2 Answers2

2

This would be the maven repackage approach:

  1. create a maven project based on the pom.xml below
  2. add the Java class Rasterizer
  3. add the JUnit test TestRasterizer
  4. add the sample File readandblue.svg
  5. run mvn install

You should get a "shaded" jar file target/com.bitplan.rasterizer-0.0.1.jar which already has the java.awt.headless call included:

You could also use the pom.xml "standalone" and simply change the main class in there.

Rasterize.java wrapper class

package com.bitplan.rasterizer;

import org.apache.batik.apps.rasterizer.Main;

/**
 * Rasterizer main class
 * https://stackoverflow.com/questions/13112967/how-to-call-batik-rasterizer-jar
 * @author wf
 *
 */
public class Rasterizer {
    /**
     * wrapper entry point for batik rasterizer
     * @param args
     */
  public static void main(String[] args) {
    System.setProperty("java.awt.headless", "true");
    // see 
    // http://www.docjar.org/html/api/org/apache/batik/apps/rasterizer/Main.java.html
    Main main=new Main(args);
    main.execute();
  }
}

TestRasterizer.java JUnit testcase

package com.bitplan.rasterizer;

import static org.junit.Assert.*;

import java.io.File;

import org.junit.Test;

/**
 * test the rasterizer
 * @author wf
 *
 */
public class TestRasterizer {

    @Test
    public void testRasterizer() {
        File testFile=new File("src/test/data/redandblue.svg");
        File pngFile=new File("src/test/data/redandblue.png");
        if (pngFile.exists())
            pngFile.delete();
        assertTrue(testFile.exists());
        String [] args={"-scriptSecurityOff","-m","image/png","-q","0.8",testFile.getPath()};
        com.bitplan.rasterizer.Rasterizer.main(args);
        assertTrue(pngFile.exists());
    }

}

sample svg file: readandblue.svg

<svg xmlns="http://www.w3.org/2000/svg"
     width="467" height="462">
  <rect x="80" y="60" width="250" height="250" rx="20"
      style="fill:#ff0000; stroke:#000000;stroke-width:2px;" />

  <rect x="140" y="120" width="250" height="250" rx="40"
      style="fill:#0000ff; stroke:#000000; stroke-width:2px;
      fill-opacity:0.7;" />
</svg>

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.bitplan.java</groupId>
    <version>0.0.1</version>
    <artifactId>com.bitplan.rasterizer</artifactId>
    <name>com.bitplan.rasterizer</name>
    <description>Provide rasterizer access</description>
    <properties>
        <junit3.version>3.8.1</junit3.version>
        <junit4.version>4.11</junit4.version>
    </properties>
    <dependencies>
        <!-- batik rasterizer -->
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-rasterizer</artifactId>
            <version>1.7</version>
        </dependency>
        <!--  https://issues.apache.org/bugzilla/show_bug.cgi?id=44682 -->
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-codec</artifactId>
            <version>1.7</version>
        </dependency>
        <!-- Runtime Junit access -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit4.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <!--  <mainClass>org.apache.batik.apps.rasterizer.Main</mainClass> -->
                                    <mainClass>com.bitplan.rasterizer.Rasterizer</mainClass> -->
                                </transformer>
                            </transformers>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
Community
  • 1
  • 1
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
1

This script gets all necessary jars from the maven repository into a classpath variable. Probably there is a better way e.g. repacking using maven ...

#!/bin/bash
#set -x
batikdir=$HOME/.m2/repository/org/apache/xmlgraphics/batik-rasterizer/1.7/
batik=batik-rasterizer-1.7.jar
main=org.apache.batik.apps.rasterizer.Main
jarlist=/tmp/jarlist$$.txt
cp=$batikdir/$batik
find $HOME/.m2/repository -type f -name *.jar > $jarlist
for j in batik-ext-1.7.jar batik-dom-1.7.jar batik-css-1.7.jar batik-svg-dom-1.7.jar batik-gvt-1.7.jar batik-parser-1.7.jar batik-script-1.7.jar batik-bridge-1.7.jar batik-anim-1.7.jar batik-transcoder-1.7.jar batik-awt-util-1.7.jar batik-codec-1.7.jar batik-util-1.7.jar batik-xml-1.7.jar xerces-2.5.0.jar xalan-2.6.0.jar xml-apis-1.3.04.jar xml-apis-ext-1.3.04.jar fop-0.94.jar batik-js.jar
do
  #echo $j
    p=`grep $j $jarlist`
    cp="$cp:$p"
done
#echo $cp
# http://tech.groups.yahoo.com/group/svg-developers/message/47939
java -cp $cp $main -scriptSecurityOff -m image/png -q 0.8 $1 
rm -f $jarlist

to get the libraries i created a dummy maven project with the following pom.xml:

  <?xml version="1.0" encoding="UTF-8"?>
    <!--
    install maven 2 to use this pom file
    run e.g. mvn dependency:go-offline to download all libraries
    -->
    <project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.bitplan.svg</groupId>
    <artifactId>com.bitplan.svg</artifactId>
    <version>0.0.1</version>
    <name>Dummy Project for SVG </name>
    <packaging>pom</packaging>
    <dependencies>
      <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-rasterizer</artifactId>
        <version>1.7</version>
        </dependency>       
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
                <artifactId>batik-codec</artifactId>
                <version>1.7</version>
        </dependency>
    </dependencies>
    <!-- configure jar plugin to build test-jar http://maven.apache.org/guides/mini/guide-attached-tests.html -->
    <build>
        <extensions>
            <extension>
                <groupId>org.apache.maven.wagon</groupId>
                <artifactId>wagon-ssh</artifactId>
                <version>1.0</version>
            </extension>
        </extensions>
    </build>
    </project>
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
  • really smart and creative! it didn't work out of the box for me, I had to also trigger the dependency download by mvn, like here: http://stackoverflow.com/a/7912247/3057265 – snowdragon Feb 03 '14 at 22:05