Java Program to Extract Content from a Java’s .class File
In this article, we are going to extract the contents of the Java class file using the Apache Tika library. Apache Tika is used for document type detection and content extraction from various file formats. It uses various document parsers and document type detection techniques to detect and extract data. It provides a single generic API for parsing different file formats. All these parser libraries are encapsulated in a single interface called the Parser interface.
The following table shows a description of the important methods used in the solution :
Method | Description |
---|---|
BodyContentHandler() | It creates a content handler that writes XHTML body character events to an internal string buffer. |
Metadata() | It constructs new, empty metadata. |
ParseContext() | It creates a parse context object that is used to pass context information to Tika parsers. |
parse() | Instantiate the parser object, and invoke the parse method. |
Example: Java code to extract the contents of Java class file format
Java
// Java program to extract the // contents of Java class file // format import java.io.File; import java.io.FileInputStream; import java.io.IOException; // importing Apache Tika libraries import org.apache.tika.exception.TikaException; import org.apache.tika.metadata.Metadata; import org.apache.tika.parser.AutoDetectParser; import org.apache.tika.parser.ParseContext; import org.apache.tika.parser.Parser; import org.apache.tika.sax.BodyContentHandler; import org.xml.sax.SAXException; public class ParserExtraction { public static void main( final String[] args) throws IOException, SAXException, TikaException { // create a File object File f = new File( "AddTwoNumbers.java" ); // parse method parameters Parser parser = new AutoDetectParser(); // instantiate BodyContentHandle BodyContentHandler handler = new BodyContentHandler(); // Creates the Metadata object Metadata metadata = new Metadata(); FileInputStream inputstream = new FileInputStream(f); // creates a parse context object ParseContext context = new ParseContext(); // parsing the file parser.parse(inputstream, handler, metadata, context); // display the file content System.out.println( "File content : " + Handler.toString()); } } |
Input :
AddTwoNumbers.java
Output:
File content : public class AddTwoNumbers { public static void main(String[] args) { int num1 = 5, num2 = 15, sum; sum = num1 + num2; System.out.println("Sum of these numbers: "+sum); } }