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
Once you have JD-Eclipseinstalled 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. BizTalk动手实验(十一)自定义开发管道组件
  2. 受限玻尔兹曼机(RBM)学习笔记(四)对数似然函数
  3. Contoso 大学 - 1 - 为 ASP.NET MVC 应用程序创建 EF 数据模型
  4. Codeforces Round #242 (Div. 2) A~C
  5. 【慕课网学习笔记】Java共享变量的可见性和原子性
  6. 第16章 网络IPC:套接字总结
  7. CLOUDSTACK接管VCENTER,意外频出,但最终搞定
  8. Android入门之login设计
  9. CSS3弹性伸缩布局(下)——flex布局
  10. 第三章 go语言 程序的流程控制
  11. alpha冲刺第九天
  12. jquery左右折叠框
  13. Python3的桌面程序开发利器:Eric6的环境搭建、使用
  14. Node 多页面请求
  15. Vue.js学习和第一个实例
  16. GitLab问题小结
  17. JQUERY方法给TABLE动态增加行
  18. maven项目在idea下右键不出现maven的解决办法
  19. 【BZOJ 3560】 3560: DZY Loves Math V (欧拉函数)
  20. MySQL数据库的主从同步复制配置

热门文章

  1. [Unity 3D] Unity 3D 里的碰撞检测
  2. 在CTime类中重载<<和>>
  3. levelDB跳表实现
  4. uboot编译: uboot编译配置和编译过程
  5. C#构架之基础学习----动态添加窗体和 控件
  6. BZOJ 1833: [ZJOI2010]count 数字计数( dp )
  7. HTML文档类型声明的坑...
  8. 开发快很重要——如果只看法语或者产品结果C++似乎很强大,但是参与这个C++的开发过程,就会感觉到这种痛苦(Google也是这个看法)
  9. sql2008R2sp1局域网镜像环境实操(无见证服务器)
  10. Repeater控件