我正在使用apache Tika 1.0。使用ForkParser,每当我解析pdf文件时,我都会得到以下NoClassDefFoundException:
java.lang.NoClassDefFoundError: org/apache/tika/fork/MemoryURLStreamHandler$Record
at org.apache.tika.fork.MemoryURLStreamHandler.createURL(MemoryURLStreamHandler.java:46)
at org.apache.tika.fork.ClassLoaderProxy.findResource(ClassLoaderProxy.java:73)
at java.lang.ClassLoader.getResource(ClassLoader.java:977)
at org.apache.log4j.helpers.Loader.getResource(Loader.java:96)
at org.apache.log4j.LogManager.<clinit>(LogManager.java:105)
at org.apache.log4j.Logger.getLogger(Logger.java:104)
at org.apache.commons.logging.impl.Log4JLogger.getLogger(Log4JLogger.java:289)
at org.apache.commons.logging.impl.Log4JLogger.<init>(Log4JLogger.java:109)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.apache.commons.logging.impl.LogFactoryImpl.createLogFromClass(LogFactoryImpl.java:1116)
at org.apache.commons.logging.impl.LogFactoryImpl.discoverLogImplementation(LogFactoryImpl.java:914)
at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:604)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:336)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:310)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685)
at org.apache.pdfbox.pdfparser.BaseParser.<clinit>(BaseParser.java:58)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1087)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1053)
at org.apache.tika.parser.pdf.PDFParser.parse(PDFParser.java:80)
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:242)
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:242)
at org.apache.tika.parser.AutoDetectParser.parse(AutoDetectParser.java:120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tika.fork.ForkServer.call(ForkServer.java:136)
at org.apache.tika.fork.ForkServer.processRequests(ForkServer.java:116)
at org.apache.tika.fork.ForkServer.main(ForkServer.java:64)
检查jar会显示tika-core jar文件中存在MemoryURLStreamHandler $ Record。当我使用AutoDetectParser而不是ForkParser时,我能够从文件中提取元数据而没有任何问题,但需要能够约束Tika内存使用,因此我需要使用ForkParser。如何才能获得pdf解析与Tika的ForkParser一起使用?
这是我解析的地方的代码片段:
public static FileAnalysis analyze(File f) throws java.io.FileNotFoundException{
FileInputStream fis = null;
ToXMLContentHandler contentHandler = new ToXMLContentHandler();
Metadata metadata = new Metadata();
ParseContext context = new ParseContext();
ForkParser parser = new ForkParser();
parser.setJavaCommand(props.getProperty("forkJavaCommand", "java") + " " +
props.getProperty("forkJavaMemory", "-Xmx64m"));
parser.setPoolSize(1);
fis = new FileInputStream(f);
try {
parser.parse(fis, contentHandler, metadata, context);
} catch (Throwable e) {
logger.error("Exception while analyzing file\n" +
"CAUTION: metadata may still have useful content in it!\n" +
"Exception:\n" + e, e);
}
我使用“-f”选项测试了Tika 1.0和Tika 0.10 CLI应用程序,并在使用SoyLatte java 6端口用于Mac OS-X时收到了IOException(Broken Pipe)。该端口仅在我的开发机器上运行,因此我使用“-f”开关在linux测试机器上运行CLI应用程序(1.0和0.10),如下所示
java -jar tika-app-1.0.jar -f /path/to/my/file.pdf
我不再收到异常,但我也没有得到任何输出。我发现这很奇怪,但是认为它可能仍然有效,只是没有产生任何输出(我猜是永远是乐观主义者)。
我取消了我的Mac OS-X终端中的所有环境变量,并尝试运行Tika CLI,就像上面使用OS-X内置的java 6.我得到了与linux测试机相同的结果,一些换行获取打印,但没有别的。我尝试了一个jpg文件而不是pdf文件,tika应用程序打印出xhtml文档,其中包含所宣传的元数据!我接下来尝试了一个docx文件,但是像pdfs一样,不打印任何内容。
我编写了一个小型测试java程序,并将其放在我们应用程序的上下文之外,以便它在新的环境中运行。
import java.io.File;
import java.io.FileInputStream;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.fork.ForkParser;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.sax.ToXMLContentHandler;
import org.apache.tika.Tika;
public class ForkParserTest {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("must be passed the file to be parsed as the first argument");
return;
}
try {
File f = new File(args[0]);
FileInputStream fis = null;
ToXMLContentHandler contentHandler = new ToXMLContentHandler();
Metadata metadata = new Metadata();
ParseContext context = new ParseContext();
ForkParser parser = new ForkParser();
fis = new FileInputStream(f);
parser.parse(fis, contentHandler, metadata, context);
System.out.println(contentHandler.toString());
} catch (Exception e) {
System.out.println("Exception caught in main");
e.printStackTrace();
}
return;
}
}
我这样编译了
javac -cp /path/to/tika-app-1.0.jar ForkParserTest.java
并且像这样跑
java -cp /path/to/tika-app-1.0.jar:${PWD} ForkParserTest /path/to/file.pdf
并用jpeg进行测试。它的表现与Tika CLI应用程序完全相同,它为jpg打印XHTML文档,但不为pdf或docx文件打印任何内容。
如果有人知道如何解决此问题,请告诉我们!此外,如果您对pdf文件或docx文件运行此测试并实际获得打印结果,请告诉我您是如何做到的。
谢谢!
我也很新发布在stackoverflow上,如果这完全是tl;博士,反馈很感激,请给我建议如何使这更简洁。