The Wayback Machine - https://web.archive.org/web/20240709152536/https://www.geeksforgeeks.org/java-program-to-extract-content-from-a-javas-class-file/
Open In App

Java Program to Extract Content from a Java’s .class File

Last Updated : 02 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 :

Extract Java program using Apache Tika

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);
  }
}


Previous Article
Next Article

Similar Reads

Java Program to Extract Content from a ODF File
The full of ODF is Open Document Format. it is an international family of standards that's the successor of commonly used deprecated vendor-specific document formats like .doc, .wpd, .xls . ODF documents are smaller when compared to other formats. OpenDocumentParser class is used from TIKA library to extract the content from the ODF file. Methods u
3 min read
Java Program to Read Content From One File and Write it into Another File
File handling plays a major role in doing so as the first essential step is writing content to a file. For this is one must know how to write content in a file using the FileWriter class. The secondary step is reading content from a file and print the same. For this, one must have good hands on File Reader class to do so. Now in order to read conte
5 min read
How to Extract File Extension From a File Path String in Java?
In Java, working with Files is common, and knowing how to extract file extensions from file paths is essential for making informed decisions based on file types. In this article, we will explore techniques for doing this efficiently, empowering developers to improve their file-related operations. Program to extract file extension from a file path S
5 min read
Java Program to Extract Content From a XML Document
An XML file contains data between the tags so it is complex to read the data when compared to other file formats like docx and txt. There are two types of parsers which parse an XML file: Object-Based (e.g. D.O.M)Event-Based (e.g. SAX, StAX) In this article, we will discuss how to parse XML using Java DOM parser and Java SAX parser. Java DOM Parser
6 min read
Java Program to Extract Content from a HTML document
HTML is the core of the web, all the pages you see on the internet are HTML, whether they are dynamically generated by JavaScript, JSP, PHP, ASP, or any other web technology. Your browser actually parses HTML and render it for you But if we need to parse an HTML document and find some elements, tags, attributes or check if a particular element exis
4 min read
Java Program to Extract Content from a PDF
Java class< file using the Apache Tika< library 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 encapsula
3 min read
Java Program to Extract Content from a Excel sheet
Spreadsheets are the easier way to represent table like data and can give a visual representation of data in tabular format. In this article, let us see how to extract the contents of the Excel sheet via java. Here there arise two cases where Maven is considered in the program or not. Discussing both of them as a prerequisite for a better understan
10 min read
Java Program to Extract Content from a TXT document
Java class< file using the Apache Tika library 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
3 min read
Different Ways to Copy Content From One File to Another File in Java
In Java, we can copy the contents of one file to another file. This can be done by the FileInputStream and FileOutputStream classes. FileInputStream Class It is a byte input stream class which helps in reading the bytes from a file. It provides different methods to read the data from a file. FileInputStream fin = new FileInputStream(filename); This
3 min read
How to Extract Content From a Text Document using Java?
There are several ways present in java to read the text file like BufferReader, FileReader, and Scanner. Each and every method provides a unique way of reading the text file. Methods: Using Files classUsing FileReader classUsing BufferReader classUsing Scanner class Let's see each and every method in detail with an example to get a better understan
10 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg