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
 
 
 
 
 

How to decompile class file in Java and Eclipse - Javap command example(转)的更多相关文章

  1. Read / Write Excel file in Java using Apache POI

    Read / Write Excel file in Java using Apache POI 2014-04-18 BY DINESH LEAVE A COMMENT About a year o ...

  2. 出现错误:Unable to load configuration. - action - file:/E:/Java/Tomcat7.0/apache-tomcat-7.0.68-windows-x64/apache-tomcat-7.0.68/webapps/SSH2Integrate/WEB-INF/classes/struts.xml:8:43

    严重: Exception starting filter struts2 Unable to load configuration. - action - file:/E:/Java/Tomcat7 ...

  3. How to Read, Write XLSX File in Java - Apach POI Example---reference

    No matter how Microsoft is doing in comparison with Google, Microsoft Office is still the most used ...

  4. [Java] Create File with java.io.File class

    Create a file with some content in some specific location. The reference is here. /** * Write fileCo ...

  5. android studio# jdk8# class file for java.lang.invoke.MethodType not found

    https://github.com/evant/gradle-retrolambda/issues/23 class file for java.lang.invoke.MethodType not ...

  6. Apache POI – Reading and Writing Excel file in Java

    来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, ...

  7. [Training Video - 6] [File Reading] [Java] Create and Write Excel File Using Apache POI API

    package com.file.properties; import java.io.File; import java.io.FileNotFoundException; import java. ...

  8. The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object

    The project was not built since its build path is incomplete. Cannot find the class file for java.la ...

  9. JAVA Error:The project was not built since its build path is incomplete. Cannot find the class file for java.util.Map$Entry.....

    今天,学习Netty框架时遇到error:Description Resource Path Location Type:The project was not built since its bui ...

随机推荐

  1. 【C语言学习】存储类型

    C语言中的存储类型主要有四种:auto.static.extern.register ★auto存储类型 默认的存储类型.在C语言中,假设忽略了变量的存储类型,那么编译器就会自己主动默认为auto型 ...

  2. 一道TOPK问题

    今天遇到一道TOP k的变形题,题目大概意思是有10W个随机整数,然后对这些数进行如下操作: 1.当能被3整除时,将此数替换为此数和其它数两两相加的数,包括数本身 2.当不能被3整除时,将此数替换为原 ...

  3. Asp.Netserver控件开发的Grid实现(三)列编辑器

    以下是GridColumnsEditor的实现代码: GridColumnsEditor.cs using System; using System.Collections.Generic; usin ...

  4. OC-多线程GCD的使用细节

    1>GCD,全称Grand Central Dispatch,”牛逼的中枢调度器”,纯C语言,提供了非常多强大的函数2>GCD优势:(1)GCD是苹果公司为多核的并行运算提出的解决方案(2 ...

  5. [Swust OJ 1026]--Egg pain's hzf

      题目链接:http://acm.swust.edu.cn/problem/1026/     Time limit(ms): 3000 Memory limit(kb): 65535   hzf ...

  6. Chapter 10 模版方法模式

    我们要完成在某一细节层次一致的一个过程或一系列步骤,但其个别步骤在更详细的层次上的实现可能不同时,我们通常考虑用模版模式来处理. 模版方法模式:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模 ...

  7. Python 2.7 学习笔记 模块和包

    我们来考虑下如下几种场景: 1.编写一个python程序,如果程序比较简单,则可以把代码放到一个python文件中.但如果程序功能比较多,可能需要多个python文件来组织源代码.而这些文件之间的代码 ...

  8. 数据库迁移 - SQLServer->MySQL

    SqlServer转换为Mysql的一款工具推荐(mss2sql)

  9. ListView与DataTable传递数据

    转载自:http://blog.sina.com.cn/s/blog_4b3485000100prhl.html 代码: using System; using System.Collections. ...

  10. SQL - 将NULL设置为 NOT NULL

    在有些时候,我们在创建表的时候,会忘掉设置某一个列的属性,比如忘了设置为非空,但是默认情况下系统会自动的设置为NULL.这样我们该怎样通过语句设置呢?? 例如有一个表,其中的一个属性为WM CHAR( ...