Class File Editor in Java

Rupam Yadav Oct 12, 2023
  1. Features of Java Class File Editor
  2. Using Java Class File Editor to Edit a Compiled Java Class
Class File Editor in Java

In this article, we will discuss the Java Class File Editor, a tool created in Java used to edit Java compiled classes. We can decompile and see the Java classes once they are created, but we need tools like the Java Class File Editor to modify them.

Features of Java Class File Editor

  • Easy to use Interface built using Java Swing
  • Allows modifications of various parts of a class file, like methods, strings, constants, etc.
  • Consistency Checks

Using Java Class File Editor to Edit a Compiled Java Class

  • Download Java Class File Editor from https://sourceforge.net/projects/classeditor/files/
  • Extract the compressed file, and open the JAR file name ce.jar shown below.

    Class File Editor in Java - Step 2
    If we are unable to open the file, we can open the command line/terminal and use the following command:

    java - jar c : ce.jar
    
  • Once the editor is opened, we open a Java class file using the File > Open menu item and choose the explorer class file.
  • We will use the following code in the class file.
    public class ExampleClass1 {
      private static final String METHOD_NAME1 = "exampleMethod1";
      private static final String METHOD_NAME2 = "exampleMethod2";
    
      public static void main(String[] args) {
        int abc = 200;
        System.out.println(abc);
        exampleMethod2();
      }
    
      static void exampleMethod2() {
        System.out.println("This is just a method");
      }
    }
    
  • After the editor opens the file, we turn on the Modify Mode in the top-right corner; this mode is off by default, as shown in the image below. The button will turn blue if it is on and green if it is off.

    Class File Editor in Java - Step 5

  • After the modification is on, we can modify certain aspects of the class by going to different editor sections.

    The General section of the editor shows the class name and its parent class. We can see and edit the interfaces if any are in the class. Class Access Modifiers are also there, which we can change if we want to.
    Class File Editor in Java - Step 6
    The next section is the Constant Pool section, which displays all the editor’s constants in the class. Here we can change the type of the constant and its value. We can add new constants or delete an existing one.
    Class File Editor in Java - Step 6
    The Fields section of the editor shows the fields with their access modifiers which we can modify.
    Class File Editor in Java - Step 6
    The last section of the editor part is the Methods section, where all the class methods are listed with their name, access modifiers, and return types that are all editable, and we can change them.
    Class File Editor in Java - Step 2

  • After all the modifications, we save the class file using the File > Save menu item. We can also export all the items like the name of fields, constants, methods, and their values as an XML file using the File > Export to XML menu item.
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn