TikaConfig.getDefaultConfig()的NoSuchMethodError错误

时间:2019-06-19 13:42:45

标签: apache-tika

“ Tika in Action”一书随附的以下示例代码给我带来了错误 Chris Mattmann和Jukka Zitting撰写:

package tikainaction.chapter2;

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.detect.Detector;
import org.apache.tika.language.LanguageIdentifier;
import org.apache.tika.language.LanguageProfile;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeTypes;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;

/**
 * Demonstrates how to call the different components within Tika: its
 * {@link Detector} framework (aka MIME identification and repository),
 *    its
 * {@link Parser} interface, its {@link LanguageIdentifier} and other
 *    goodies.
 */
//<start id="my_first_tika"/>

public class MyFirstTika {

  public static void main(String[] args) throws Exception {
    String filename = args[0];
    MimeTypes mimeRegistry =
                   TikaConfig.getDefaultConfig().getMimeRepository();
    //<co id="get_mime_repo" />

    System.out.println("Examining: [" + filename + "]");

    System.out.println("The MIME type (based on filename) is: ["
        + mimeRegistry.getMimeType(filename) + "]");
    //<co id="guess_mime_filename" />

    System.out.println("The MIME type (based on MAGIC) is: ["
        + mimeRegistry.getMimeType(new File(filename)) + "]");
    //<co id="guess_mime_magic" />

    Detector mimeDetector = (Detector) mimeRegistry;
    System.out.println(
        "The MIME type (based on the Detector interface) is: ["
        + mimeDetector.detect(new File(filename).toURL().openStream(),
            new Metadata()) + "]");//<co id="guess_detector" />


    LanguageIdentifier lang = new LanguageIdentifier(new LanguageProfile(
        FileUtils.readFileToString(new File(filename))));
    //<co id="lang_detect"/>

    System.out.println("The language of this content is: ["
        + lang.getLanguage() + "]");

    Parser parser = TikaConfig.getDefaultConfig().getParser( // This is line 59
            MediaType.parse(
                mimeRegistry.getMimeType(filename).getName()));//<co id="parse"/>
    Metadata parsedMet = new Metadata();
    ContentHandler handler = new BodyContentHandler();
    parser.parse(new File(filename).toURL().openStream(), handler,
        parsedMet, new ParseContext());


    System.out.println("Parsed Metadata: ");
    System.out.println(parsedMet);
    System.out.println("Parsed Text: ");
    System.out.println(handler.toString());

  }
}
//<end id="my_first_tika"/>

我得到的错误是:

线程“主”中的异常java.lang.NoSuchMethodError:org.apache.tika.config.TikaConfig.getParser(Lorg / apache / tika / mime / MediaType;)Lorg / apache / tika / parser / Parser ;   在tikainaction.chapter2.MyFirstTika.main(MyFirstTika.java:59)

为方便起见,我在源代码中标记了第59行。

我遇到了tika-app-1.0.jar和tika-app-1.21.jar错误。

0 个答案:

没有答案