[英]How to call batik-rasterizer.jar
Using batik-rasterizer.jar
from version 1.7 of batik, I was wondering how to call the jar correctly. 使用
batik-rasterizer.jar
1.7版本的batik-rasterizer.jar
,我想知道如何正确调用jar。
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. 因此,我一直在寻找正确使用的
MainClass
和ClassPath
。 I'll post a script I came up with as an answer in a minute ... 我会在一分钟内发布一个我想出的脚本作为答案......
This would be the maven repackage approach: 这将是maven重新包装的方法:
You should get a "shaded" jar file target/com.bitplan.rasterizer-0.0.1.jar which already has the java.awt.headless call included: 你应该得到一个“阴影”的jar文件target / com.bitplan.rasterizer-0.0.1.jar,它已经包含了java.awt.headless调用:
You could also use the pom.xml "standalone" and simply change the main class in there. 你也可以使用pom.xml“standalone”,只需更改那里的主类。
Rasterize.java wrapper class 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 testcase 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());
}
}
sample svg file: readandblue.svg 示例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: 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>
This script gets all necessary jars from the maven repository into a classpath variable. 该脚本将所有必需的jar从maven存储库中获取到classpath变量中。 Probably there is a better way eg repacking using 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
to get the libraries i created a dummy maven project with the following pom.xml: 获取库我使用以下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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.