朋友您好,我尝试以编程方式使用fop引擎,我搜索了一个示例,发现了此类
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
import org.apache.avalon.framework.ExceptionUtil;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.FOPException;
import org.apache.fop.messaging.MessageHandler;
public class Invokefop
{
public void convertXML2PDF(File xml, File xslt, File pdf)
throws IOException, FOPException, TransformerException {
Driver driver = new Driver();
Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
driver.setLogger(logger);
MessageHandler.setScreenLogger(logger);
driver.setRenderer(Driver.RENDER_PDF);
OutputStream out = new java.io.FileOutputStream(pdf);
try {
driver.setOutputStream(out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xslt));
Source src = new StreamSource(xml);
Result res = new SAXResult(driver.getContentHandler());
transformer.transform(src, res);
} finally {
out.close();
}
}
public static void main(String[] args) {
try {
System.out.println("JAVA XML2PDF:: FOP ExampleXML2PDF\n");
System.out.println("JAVA XML2PDF:: Preparing...");
File base = new File("../");
File baseDir = new File(base, "in");
File outDir = new File(base, "out");
outDir.mkdirs();
File xmlfile = new File(baseDir, args[0]);
File xsltfile = new File(baseDir, args[1]);
File pdffile = new File(outDir, args[2]);
System.out.println("JAVA XML2PDF:: Input: XML (" + xmlfile + ")");
System.out.println("JAVA XML2PDF:: Stylesheet: " + xsltfile);
System.out.println("JAVA XML2PDF:: Output: PDF (" + pdffile + ")");
System.out.println();
System.out.println("JAVA XML2PDF:: Transforming...");
Invokefop app = new Invokefop();
app.convertXML2PDF(xmlfile, xsltfile, pdffile);
System.out.println("JAVA XML2PDF:: Success!");
} catch (Exception e) {
System.err.println(ExceptionUtil.printStackTrace(e));
System.exit(-1);
}
}
}
Fop中的所有Lib都在Classpath中,包括build目录中的fop.jar。后
我运行
javac Invokefop.java
我收到此错误:> C:\....\fop>javac Invokefop.java
Invokefop.java:21: cannot find symbol
symbol : class Driver
location: package org.apache.fop.apps
import org.apache.fop.apps.Driver;
^
Invokefop.java:23: package org.apache.fop.messaging does not exist
import org.apache.fop.messaging.MessageHandler;
^
Invokefop.java:31: cannot find symbol
symbol : class Driver
location: class Invokefop
Driver driver = new Driver();
^
Invokefop.java:31: cannot find symbol
symbol : class Driver
location: class Invokefop
Driver driver = new Driver();
^
Invokefop.java:36: cannot find symbol
symbol : variable MessageHandler
location: class Invokefop
MessageHandler.setScreenLogger(logger);
^
Invokefop.java:39: cannot find symbol
symbol : variable Driver
location: class Invokefop
driver.setRenderer(Driver.RENDER_PDF);
^
6 errors
我是Java的新手,但是通过这种方法,我尝试使用此Java类在c ++中执行fop引擎。
有任何想法,如何解决这个错误...
提前感谢。
最佳答案
fop.jar
文件中不存在出现“找不到符号”错误的类。
我刚刚下载了FOP库的v0.95版本,但在发行版的任何jar文件中都看不到这些类。 (您可以在任何zip程序中打开jar文件并进行查看-jar基本上是一个zip文件,其中包含Java使用的一些额外位)。
我想您的示例代码中引用的类在某个阶段已从FOP库中删除,并且该示例适用于旧版本。因此,您可以尝试查找包含这些类的较旧版本的库,或查找较新的示例。二进制发行版附带一些示例-也许看看这些示例,看看是否对您有用?
关于java - 找不到(想)的软件包,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2682927/