1

从 1.7 版开始使用batik-rasterizer.jarbatik,我想知道如何正确调用 jar。

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

这给了我:

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

因此,我一直在寻找正确的MainClassClassPath使用。我会在一分钟内发布一个我想出的脚本作为答案......

4

2 回答 2

2

这将是 Maven 重新打包方法:

  1. 根据下面的 pom.xml 创建一个 maven 项目
  2. 添加 Java 类 Rasterizer
  3. 添加 JUnit 测试 TestRasterizer
  4. 添加示例文件 readandblue.svg
  5. 运行 mvn 安装

你应该得到一个“阴影”jar 文件 target/com.bitplan.rasterizer-0.0.1.jar 已经包含了 java.awt.headless 调用:

您也可以使用 pom.xml “独立”并简单地更改其中的主类。

Rasterize.java 包装类

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 测试用例

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());
    }

}

示例 svg 文件: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>
于 2014-10-30T14:21:49.890 回答
1

该脚本将所有必要的 jar 从 maven 存储库中获取到类路径变量中。可能有更好的方法,例如使用 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

为了获取库,我使用以下 pom.xml 创建了一个虚拟 maven 项目:

  <?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>
于 2012-10-28T21:03:00.273 回答