In this program, you will see complete steps to extract content and metadata from a class file by TIka ClassParser.
Sample File

Complete Example
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.tika.exception.TikaException; import org.apache.tika.metadata.Metadata; import org.apache.tika.parser.ParseContext; import org.apache.tika.parser.asm.ClassParser; import org.apache.tika.sax.BodyContentHandler; import org.xml.sax.SAXException; public class TikaJavaClassParsingExample { public static void main(String[] args) throws IOException, SAXException, TikaException { //detecting the file type BodyContentHandler handler = new BodyContentHandler(); Metadata metadata = new Metadata(); FileInputStream inputstream = new FileInputStream(new File("C:\\Users\\Saurabh Gupta\\Desktop\\TIKA\\TikaTest.class")); ParseContext pcontext = new ParseContext(); //Html parser ClassParser ClassParser = new ClassParser(); ClassParser.parse(inputstream, handler, metadata); System.out.println("Contents of the Java Class File:\n" + handler.toString()); System.out.println("Metadata of the Java Class File:\n"); String[] metadataNames = metadata.names(); for(String name : metadataNames) { System.out.println(name + " : " + metadata.get(name)); } } }
Output
Contents of the Java Class File:
package com.fiot.tika.examples;
public class TikaTest {
public static void main(String[] args) {
System.out.println("You are in FacingIssuesOnIT.");
System.out.println("Learn from Others Experience.");
}
}
Metadata of the Java Class File:
dc:title : TikaTest
resourceName : TikaTest.class
title : TikaTest
You must be logged in to post a comment.