Using Tika as a Library (Java API)

Tika embeds directly in your Java application, giving you full control over parsing, detection and configuration.

Some file formats can trigger excessive memory use, infinite loops, or JVM crashes in the underlying parsing libraries. For production systems processing untrusted files, use Tika Pipes, which runs each parse in a forked JVM with timeouts and memory limits; tika-server and tika-grpc provide the same robustness as a service. See Getting Started with the Java API for how to choose, and Robustness for what goes wrong without isolation.

Know where the line falls before you call AutoDetectParser in your own JVM. Tika’s sandboxing is the forked JVM, and calling a parser directly opts out of it. Untrusted data can often be crafted to exhaust memory or CPU, or to crash the process; containing that is then the caller’s responsibility, and the project does not treat it as a security issue. The other half holds too: a document that defeats Tika Pipes' own limits is a bug in Tika, and is treated as a security issue. The remedy is concrete rather than "be careful" — parse through Pipes and the limits are Tika’s problem again. See Security for the full model.

Dependencies

tika-parsers-standard-package pulls in tika-core and all standard parsers (PDF, Office, HTML, and the rest):

<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-parsers-standard-package</artifactId>
    <version>4.1.0-SNAPSHOT</version>
    <type>pom</type>
</dependency>

Take tika-core alone if you only need detection, or want to select parsers individually. Add tika-serialization if you want TikaLoader and JSON-based configuration:

<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-core</artifactId>
    <version>4.1.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-serialization</artifactId>
    <version>4.1.0-SNAPSHOT</version>
</dependency>

Parsers

org.apache.tika.parser.Parser is Tika’s fundamental document-processing interface. One method does the work:

void parse(TikaInputStream tis,
           ContentHandler handler,
           Metadata metadata,
           ParseContext context) throws IOException, SAXException, TikaException;
  • TikaInputStream — the document content.

  • ContentHandler — receives XHTML SAX events.

  • Metadata — bidirectional: input hints (filename, content type) in, extracted metadata out.

  • ParseContext — context-specific settings injected into the parse.

The design favours streamed processing (large documents are not held entirely in memory), and structured output: parsers emit XHTML SAX events that preserve document hierarchy — headings, links, tables — rather than a flat blob.

<html>
  <head>
    <title>...</title>
  </head>
  <body>...</body>
</html>

AutoDetectParser

AutoDetectParser determines the document type and selects the appropriate parser, encapsulating all of Tika in one parser:

try (TikaInputStream stream = TikaInputStream.get(path)) {
    AutoDetectParser parser = new AutoDetectParser();
    BodyContentHandler handler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    ParseContext context = new ParseContext();

    parser.parse(stream, handler, metadata, context);

    String content = handler.toString();
    String title = metadata.get(TikaCoreProperties.TITLE);
}
new BodyContentHandler() stops after 100,000 characters and throws WriteLimitReachedException. Use new BodyContentHandler(-1) for no limit, or pass an explicit character budget. See Setting Limits for the limits that apply when parsing through Tika Pipes.
Always use TikaInputStream, and hand it the original resource — TikaInputStream.get(path) for a Path, TikaInputStream.get(bytes) for a byte[]. That lets Tika reach the underlying resource efficiently and enables the mark/reset support many parsers and detectors require.

Content Handlers

The content handler controls the output format:

Handler Output

BodyContentHandler

Body content, as a stream or a string.

ToTextContentHandler

Plain text.

ToHTMLContentHandler

HTML.

ToXMLContentHandler

XHTML/XML.

ToMarkdownContentHandler

Markdown, preserving headings, lists, tables, code blocks, emphasis and links.

ParsingReader

Extracted text as a character stream, filled by a background thread.

BasicContentHandlerFactory creates handlers by type: TEXT, HTML, XML, BODY, MARKDOWN, IGNORE.

Key metadata properties

  • TikaCoreProperties.RESOURCE_NAME_KEY — filename or resource identifier

  • HttpHeaders.CONTENT_TYPE — declared document format (a Property; Metadata no longer re-exports the HTTP header constants)

  • TikaCoreProperties.TITLE — document title

  • TikaCoreProperties.CREATOR — document creator

Detectors

org.apache.tika.detect.Detector is the foundation of Tika’s detection system. Every detection approach implements one method, which examines the stream, the metadata and the parse context and returns a MediaType:

MediaType detect(TikaInputStream tis, Metadata metadata, ParseContext parseContext)
    throws IOException;
Approach How it works

Magic

Byte patterns near the start of the file, in Freedesktop MIME-info format. Implemented by MimeTypes over configuration files such as tika-mimetypes.xml.

Name-based

Filename patterns, via NameDetector. Quick, but unreliable if files are renamed.

Known content type

Refines detection using MIME type information already supplied by a web server or repository.

Container-aware

Inspects formats stored inside containers — OLE2 for .doc/.ppt, ZIP for iWork. Needs TikaInputStream and the Tika parsers jar.

DefaultDetector discovers and tries all available detectors via service loaders: magic and name detection with tika-core alone, plus container detection when the parsers are on the classpath.

TikaLoader loader = TikaLoader.loadDefault();
Detector detector = loader.loadDetectors();
ParseContext parseContext = new ParseContext();

for (Path p : myListOfPaths) {
    Metadata metadata = new Metadata();

    try (TikaInputStream stream = TikaInputStream.get(p, metadata)) {
        MediaType mimetype = detector.detect(stream, metadata, parseContext);
        System.out.println("File " + p + " is " + mimetype);
    }
}
TikaInputStream.get(path, metadata) sets the resource name in the metadata for you.

Language detection is separate, through LanguageDetector extensions — useful for documents that carry no language metadata.