How to decompile class file in Java and Eclipse - Javap command example(转)

简介: Ability to decompile a Java class file is quite helpful for any Java developer who wants to look into the source of any open source or propriety library used in project.

 

Ability to decompile a Java class file is quite helpful for any Java developer who wants to look into the source of any open source or propriety library used in project. Though I always prefer to  attach source in Eclipse of most common libraries like JDK but it’s not always possible with increasing number of dependencies. Java decompiler (a program which can decompile Java class files to produce so urce files) is very helpful on such situation. By using Java decompiler you can easily check out source of any .class file. Thanks to Eclipse IDE and increasing number of free plug-in available for Java developers, You can have powerful Java decompile in your armory. Earlier I used to used JadEclipse an Eclipse plugin which works well with JAD decompiler but knowing about JAD decompiler is not supporting Java 1.5 source, I was in hunt of another suitable and useful Eclipse plugin which can decompile . class file. My search ends with JD-Eclipse , this is another free Eclipse plugin for non commercial and personal use which helps you to get source from class files. JD-Eclipse is easy to install and its site has detailed step by step guide on how to install JD-Eclipse plug-in. If you are already familiar with Eclipse plugin installation than it just a cake walk.
 
 
How to decompile Class file in Eclipse IDE
how to decompile class file in Java and Eclipse with javap command exampleOnce you have  JD-Eclipse installed and running, you probably need to restart Eclipse once you installed plug-in. You are ready to decompile Java class file from Eclipse. In order to decompile class file, just open any of your Java project and go to Maven dependencies of libraries to see the jar files included in your project. Just expand any jar file for which you don't have source attached in Eclipse and click on . class file. Now you can see the source code for that file in Java editor, simple and elegant isn't it. Though I highly recommend to attach source code for JAR in Eclipse , at least for frequently used libraries like JDK or Spring as quality of decompiled code and original code is different. Original code gives better visibility and also shows comment while decompiled code is just bare minimum version.
 

How to decompile class file in Java – javap command example

Even with powerful Eclipse IDE and plugin, we may some time needs t  o work on command prompt esp ecially while working in Linux development servers and its not convenient to switch back and fourth for quick look on .class file or get some information from compiled form of source. Thanks to javap command you can decompile class file on the fly in command prompt. javap is standard binary which comes with JDK installation and resides in JAVA_HOME/bin directory. javap is similar to javac (java compiler) and work directly with .class file. In order to use javap command you must hav e JAVA_HOME in your system path. you can verify this by typing " javap " in command prompt and if it doesn't complain and instead show some output like below than you are good to go. I f it doesn't recognize the command means you need to set path, check how to set path in java more detailed steps.
 
abc@localhost:~/java javap  No classes were specified on the command line.   Try -help.
 
Now let's see what  javap command offers us. We have a s imple Java class wit h one field and one method.
 
abc@localhost:~/java cat Hello.java  public class Hello {          private String version= "1.2" ;          public static void main ( String args []){                  System . out . println ( "Running Hello" ) ;          }  } 
abc@localhost:~/java javap Hello  Compiled from "Hello.java"  public class Hello extends java. lang . Object {      public Hello () ;      public static void main ( java. lang . String []) ;  }
 
So it looks  javap only provides information related to method in .class file. It also stat e the constructor eve n default constructor added by Java compiler. javap can provide more information depending upon its command line option like - private will show all private members . h ere is an example of running javap command with -private option:
 
abc@localhost:~/java javap - private Hello  Compiled from "Hello.java"  public class Hello extends java. lang . Object {      private java. lang . String version ;      public Hello () ;      public static void main ( java. lang . String []) ;  }
 
 
How to see bytecode from .class file
javap   command can also show   bytecodes form compiled class files. Just run javap with -c option and it will print byte codes of   class file, as shown in below example:
 
abc@localhost:~/java javap -c Hello  Compiled from "Hello.java"  public class Hello extends java. lang . Object {  public Hello () ;    Code:      0 :   aload_0      1 :   invokespecial   # 1 ; //Method java/lang/Object." ":()V      4 :   aload_0      5 :   ldc     # 2 ; //String 1.2      7 :   putfield        # 3 ; //Field version:Ljava/lang/String;      10 :   return 
public static void main ( java. lang . String []) ;    Code:      0 :   getstatic       # 4 ; //Field java/lang/System.out:Ljava/io/PrintStream;      3 :   ldc     # 5 ; //String Running Hello      5 :   invokevirtual   # 6 ; //Method java/io/PrintStream.println:(Ljava/lang/String;)V      8 :   return 
}
 
That's all on  how to decompile class files in Java and Eclipse IDE . JD-Eclipse is an easy to use eclipse plugin and has detailed installation steps documented. if you are running on JDK lower than Java 5 than You can still use famous JAD decompiler and JADEclipse plug-in. Apart from these are many more which I haven't try. Just go to Eclipse market place and you will see lot of those.
 
Relate  Eclipse shortcuts and tutorials from Javarevisited Blog
 
 
 
 
 
相关文章
|
1月前
|
Java Android开发
Eclipse Java 构建路径
Eclipse Java 构建路径
38 3
|
1月前
|
Java 编译器 Maven
Java“class file contains wrong class”解决
当Java程序运行时出现“class file contains wrong class”错误,通常是因为类文件与预期的类名不匹配。解决方法包括:1. 确保类名和文件名一致;2. 清理并重新编译项目;3. 检查包声明是否正确。
63 3
|
2月前
|
Java 程序员 编译器
在Java编程中,保留字(如class、int、for等)是具有特定语法意义的预定义词汇,被语言本身占用,不能用作变量名、方法名或类名。
在Java编程中,保留字(如class、int、for等)是具有特定语法意义的预定义词汇,被语言本身占用,不能用作变量名、方法名或类名。本文通过示例详细解析了保留字的定义、作用及与自定义标识符的区别,帮助开发者避免因误用保留字而导致的编译错误,确保代码的正确性和可读性。
63 3
|
3月前
|
Java
java基础(4)public class 和class的区别及注意事项
本文讲解了Java中`public class`与`class`的区别和注意事项。一个Java源文件中只能有一个`public class`,并且`public class`的类名必须与文件名相同。此外,可以有多个非`public`类。每个类都可以包含一个`main`方法,作为程序的入口点。文章还强调了编译Java文件生成`.class`文件的过程,以及如何使用`java`命令运行编译后的类。
68 3
java基础(4)public class 和class的区别及注意事项
|
2月前
|
Java
让星星⭐月亮告诉你,Java synchronized(*.class) synchronized 方法 synchronized(this)分析
本文通过Java代码示例,介绍了`synchronized`关键字在类和实例方法上的使用。总结了三种情况:1) 类级别的锁,多个实例对象在同一时刻只能有一个获取锁;2) 实例方法级别的锁,多个实例对象可以同时执行;3) 同一实例对象的多个线程,同一时刻只能有一个线程执行同步方法。
22 1
|
2月前
|
小程序 Oracle Java
JVM知识体系学习一:JVM了解基础、java编译后class文件的类结构详解,class分析工具 javap 和 jclasslib 的使用
这篇文章是关于JVM基础知识的介绍,包括JVM的跨平台和跨语言特性、Class文件格式的详细解析,以及如何使用javap和jclasslib工具来分析Class文件。
59 0
JVM知识体系学习一:JVM了解基础、java编译后class文件的类结构详解,class分析工具 javap 和 jclasslib 的使用
|
3月前
|
Java
java的class类
java的class类
54 5
|
4月前
|
Java 测试技术 Docker
记录一次很坑的报错:java.lang.Exception: The class is not public.
这篇文章记录了作者在Docker中运行服务进行单元测试时遇到的一系列问题,包括Spring Boot与Spring Cloud版本不一致、Bean注入问题、测试单元引入问题以及公共类和方法的可见性问题,并提供了解决问题的方法和成功测试通过的代码示例。
记录一次很坑的报错:java.lang.Exception: The class is not public.
|
4月前
|
Java
JAVA中public class和class的区别
JAVA中public class和class的区别
59 7
|
4月前
|
Oracle Java 关系型数据库
简单记录在Linux上安装JDK环境的步骤,以及解决运行Java程序时出现Error Could not find or load main class XXX问题
本文记录了在Linux系统上安装JDK环境的步骤,并提供了解决运行Java程序时出现的"Error Could not find or load main class XXX"问题的方案,主要是通过重新配置和刷新JDK环境变量来解决。
166 0

推荐镜像

更多