Original Blog Address
Javap is a built-in tool of the JDK. You can find it in the /bin directory of the JDK installation directory. You can decomcompile the code or view the bytecode generated by the Java compiler to analyze the code execution process and understand the internal work of the JVM.
The following lists the common options of the javap command and their function descriptions. Please use Google for more functions.
Use the
-help help-lOutput rows and variable tables -public only output public methods and fields -protected only output public and protected classes and members -package only output packages, public and protected classes and members, This is the default -p -private to output all classes and members-s-verbose Output stack size, number of method parameters -Constants Output static final constantsCopy the code
The example analysis
The javap command decomposes a class file that determines what to output based on options. If options are not used, Javap will print out the packages in the class file, the protected and public fields in the class, and all the methods in the class. Javap will print them out to standard output. To look at this example, compile the following class.
package com.thundersoft.metadata.test.kafka;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.junit.Test;
import java.util.Arrays;
import java.util.Properties;
public class KafkaTest {
@Test
public void testProducer() {
Properties props = new Properties();
props.put("bootstrap.servers"."192.168.204.30:9092");
props.put("acks"."all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer"."org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer"."org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
for(int i = 0; i < 100; i++) {
producer.send(new ProducerRecord<String, String>("my-topic", Integer.toString(i), Integer.toString(i)));
}
producer.close();
}
@Test
public void testKafkaConsumer() {
Properties props = new Properties();
props.put("bootstrap.servers"."192.168.204.30:9092");
props.put("group.id"."test");
props.put("enable.auto.commit"."true");
props.put("auto.commit.interval.ms"."1000");
props.put("key.deserializer"."org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer"."org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList("my-topic"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(100);
for (ConsumerRecord<String, String> record : records)
System.out.printf("offset = %s, key = %s, value = %s%n", record.topic(), record.key(), record.value()); } } public static void main(String[] args) { int a = 2; int b = 3; int sum = a*b; System.out.println(sum); }}Copy the code
After you type Javap KafkaTest on the command line, the following output is displayed
public class com.thundersoft.metadata.test.kafka.KafkaTest {
public com.thundersoft.metadata.test.kafka.KafkaTest();
public void testProducer();
public void testKafkaConsumer();
public static void main(java.lang.String[]);
}
Copy the code
Analyze compiler execution with code
Here we’ll focus on the code logic inside the main method, which looks like this
public static void main(String[] args) {
int a = 2;
int b = 3;
int sum = a*b;
System.out.println(sum);
}
Copy the code
After you type javap -c KafkaTest on the command line, the following output is displayed
public static void main(java.lang.String[]);
Code:
0: iconst_2
1: istore_1
2: iconst_3
3: istore_2
4: iload_1
5: iload_2
6: imul
7: istore_3
8: getstatic #47 // Field java/lang/System.out:Ljava/io/PrintStream;
11: iload_3
12: invokevirtual #54 // Method java/io/PrintStream.println:(I)V
15: return
Copy the code
Iconst_2 and iconst_3 represent constants 2,3, respectively, as shown in the code above. Istore_1 and istore_2 mean to define two ordinary variables respectively. Iload_1 and ILOAD_2 mean to load istore_1 and ISTore_2 into the data stack respectively. Imul means to multiply the two variables and assign the result to the variable ISTore_3. Finally, the result is output and the program returns.
In the process of analyzing this simple code, I found a website where JVM commands are compiled and shared.
conclusion
The building Lord did a simple code analysis process above, hoping to help predestined people. Javap can be used to decompile and view the bytecode compiled by the compiler. Javap -c lists the JVM instructions executed by each method. This command is a good choice for solving tricky logic bugs. In addition, through the bytecode and source code comparison, in-depth analysis of Java compilation principle and code execution process, to solve a variety of Java principle level problems.