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 |
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.0.0</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.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-serialization</artifactId>
<version>4.0.0</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 |
|---|---|
|
Body content, as a stream or a string. |
|
Plain text. |
|
HTML. |
|
XHTML/XML. |
|
Markdown, preserving headings, lists, tables, code blocks, emphasis and links. |
|
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 (aProperty;Metadatano 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
|
Name-based |
Filename patterns, via |
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 |
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.