How to create a user defined javap tool?
Last Updated :
11 Jul, 2025
What is a javap tool?
The
javap tool is used to get the information of any class or interface. The javap command (also known as the
Java Disassembler) disassembles one or more class files. Its output depends on the
options used ("
-c" or "
-verbose" for byte code and byte code along with innards info, respectively). If no options are used, javap prints out the package, protected, and public fields and methods of the classes passed to it.
Syntax:
javap [classname] [option]
To understand it clearly, see the below example on Command prompt which prints the details of
String class
The command used is:
javap java.lang.String
Output:
How to create a user defined javap tool?
To create a user defined javap tool, we will use following methods of
java.lang.class
- Method[] getDeclaredMethods(): This method returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.
- Field[] getDeclaredFields(): This method returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields.
- Constructor[] getDeclaredConstructors(): This method returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object. These are public, protected, default (package) access, and private constructors.
We will use
Reflection API of Java also.
Reflection API is used to examine or modify the behavior of methods, classes, interfaces at runtime.
Below programs implement user defined javap tool:
Example 1: In this example we use this program on user defined class.
java
// The program implements
// user defined javap tool
// Import Reflection
import java.lang.reflect.*;
// Test class on which
// custom javap tool will be used
class test_class {
// Variables
int a;
double d;
char c;
String s;
// Constructors
test_class()
{
a = 0;
d = 0.0;
c = 'a';
s = "Hello";
}
test_class(int a, double d, char c, String s)
{
this.a = a;
this.d = d;
this.c = c;
this.s = s;
}
// Some class Methods
void printData()
{
System.out.println(a + d + c + s);
}
void setData()
{
a = 1;
d = 0.0;
c = 'A';
s = "Hello Geeks";
}
}
// Custom javap tool
public class javapcustom {
public static void main(String[] args)
throws Exception
{
Class class_name = Class.forName("test_class");
// Print field of class
System.out.println("Fields of class");
Field f[] = class_name.getDeclaredFields();
for (int i = 0; i < f.length; i++) {
System.out.println(f[i]);
}
// Print constructor of class
System.out.println("\nConstructors of class");
Constructor cons[] = class_name.getDeclaredConstructors();
for (int i = 0; i < cons.length; i++) {
System.out.println(cons[i]);
}
// Print methods of class
System.out.println("\nMethods of class");
Method method[] = class_name.getDeclaredMethods();
for (int i = 0; i < method.length; i++)
System.out.println(method[i]);
}
}
Output:
Fields of class
int test_class.a
double test_class.d
char test_class.c
java.lang.String test_class.s
Constructors of class
test_class(int, double, char, java.lang.String)
test_class()
Methods of class
void test_class.printData()
void test_class.setData()
Example 2: In this example we use this user-defined javap tool on pre-defined Java classes.
java
// The program implements
// user defined javap tool
// Import Reflection
import java.lang.reflect.*;
// Custom javap tool
public class javapcustom {
public static void main(String[] args)
throws Exception
{
// You can replace args[0] with
// the name of predefined Class
// on which you want to use javap tool
// eg: replace args[0] with "java.lang.String"
Class class_name = Class.forName(args[0]);
// Print field of class
System.out.println("Fields of class");
Field f[] = class_name.getDeclaredFields();
for (int i = 0; i < f.length; i++) {
System.out.println(f[i]);
}
// Print constructor of class
System.out.println("\nConstructors of class");
Constructor cons[] = class_name.getDeclaredConstructors();
for (int i = 0; i < cons.length; i++) {
System.out.println(cons[i]);
}
// Print methods of class
System.out.println("\nMethods of class");
Method method[] = class_name.getDeclaredMethods();
for (int i = 0; i < method.length; i++)
System.out.println(method[i]);
}
}
Output:
Similar Reads
Java | How to create your own Helper Class? In Java, a Helper class is a class that contains useful methods which make common tasks easier, like error handling, checking input, etc. This class intends to give a quick implementation of basic functions so that the programmers do not have to implement them again and again. This class is easy to
7 min read
How to Create a .exe File From a Java Program? In this article, we will learn how to create a .exe file from a Java program. Java is a platform-independent language, which works on the philosophy of "write Once, Run Anywhere". It simply means that if we write a Java program, we can run it on any device that has a JVM(Java Virtual Machine). Now i
5 min read
What is JavaDoc tool and how to use it? JavaDoc tool is a document generator tool in Java programming language for generating standard documentation in HTML format. It generates API documentation. It parses the declarations ad documentation in a set of source file describing classes, methods, constructors, and fields. Before using JavaDoc
3 min read
How to Create a Java Docker Container? Java is one of the most popular languages and supports many enterprise applications. Running Java on local machines requires the installation of Java IDE, Java JDK, and Java JRE and the setting of paths and environment variables. This might seem like a hefty task especially if you just want to run a
9 min read
How to Create an Executable JAR with Maven? The Maven is a powerful build automation tool primarily used for java program projects. And It simplifies that build process, dependency management and project configuration through a standard project object model which means POM file. This file is heart of the Maven Project and Build Automation. In
4 min read
How to Create Custom Class in Java? Class is the collection of objects. Class is not a real-world entity it is just only templates and prototypes or blueprints. Class does not occupy memory. We can write a custom class as per our choice for an illustration purpose a sample is shown in the program below as a helper class. Example: Java
2 min read