This section provides a tutorial example of how to lookup method signatures in a class file or JAR file with the 'javap' command.
"javap" can also be used as a handy tool to look up method signatures while you are writing Java programs.
For example, if you are coding a Java program, and you need to convert a byte array into a character string,
but you don't remember which method in the String class to use, and how many arguments are needed for that method.
If you are using a good Java IDE, it will help you with a list of public methods and their signatures.
But what can you do, if you are using a simple text editor, not a smart Java IDE?
You can use the "javap" command as a handy tool to look up method signatures.
The following tutorial example shows you how to find the method to convert a byte array into a character string:
C:\herong>javap java.lang.String | find "byte"
public java.lang.String(byte[], int, int, int);
public java.lang.String(byte[], int);
public java.lang.String(byte[], int, int, java.lang.String)
throws java.io.UnsupportedEncodingException;
public java.lang.String(byte[], java.lang.String)
throws java.io.UnsupportedEncodingException;
public java.lang.String(byte[], int, int);
public java.lang.String(byte[]);
public void getBytes(int, int, byte[], int);
public byte[] getBytes(java.lang.String)
throws java.io.UnsupportedEncodingException;
public byte[] getBytes();
What can you learn from this tutorial example:
"java.lang.String" is the fully qualified class name for the String class.
The bytecode of "java.lang.String" is inside the JDK JAR file: \j2sdk1.5.0\jre\lib\rt.jar.
since "rt.jar" is the default JAR file, you don't need to specify it with the "-classpath" option.
I used the Windows "find" command (similar to the Unix "grep" command) to select method signatures
that contains the word "byte" to reduce the amount of output lines.
Based on the output, the simplest method is the constructor "String(byte[])",
which converts all bytes in the specified byte array into a string using the default character encoding.