Decompile java class file to java file

How to decompile class file in Java and Eclipse — Javap command example

The 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 a project. Though I always prefer to attach sources in Eclipse of most common libraries like JDK it’s not always possible with an increasing number of dependencies. Java decompiler (a program that can decompile Java class files to produce so urce files) is very helpful in such a situation. By using Java decompiler you can easily check out the source of any .class file. Thanks to Eclipse IDE and the increasing number of free plugins available for Java developers, You can have powerful Java decompile in your armory.

Earlier I used to use JadEclipse an Eclipse plugin that works well with JAD decompiler but knowing about the 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 the source from class files. JD-Eclipse is easy to install and its site has a detailed step by step guide on how to install JD-Eclipse plug-in. If you are already familiar with Eclipse plugin installation then it just a cakewalk.

Читайте также:  Url html new windows

How to decompile Class file in Eclipse IDE

how to decompile class file in Java and Eclipse with javap command example

Once you have JD-Eclipse installed and running, you probably need to restart Eclipse once you installed the plug-in. You are ready to decompile Java class file from Eclipse. In order to decompile class file, just open any of your Java projects 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 the source attached in Eclipse and click on the .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 attaching source code for JAR in Eclipse , at least for frequently used libraries like JDK or Spring as the quality of decompiled code and the original code is different. The original code gives better visibility and also shows comment while decompiled code is just the bare minimum version.

How to decompile class file in Java – javap command example

Even with powerful Eclipse IDE and plugin, we may sometimes need t o work on command prompt esp ecially while working in Linux development servers and its not convenient to switch back and forth 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 yo ur 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.

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 a method in the .class file. It also states 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 the -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 bytecodes 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 easy to use the eclipse plugin and has detailed installation steps documented. if you are running on JDK lower than Java 5 than You can still use a famous JAD decompiler and JADEclipse plug-in. Apart from these are much more which I haven’t tried. Just go to the Eclipse marketplace and you will see a lot of those.

Источник

.JAR and .Class to Java decompiler

Until recently, you needed to use a Java decompiler and all of them were either unstable, obsolete, unfinished, or in the best case all of the above. And, if not, then they were commercial. The obsoleteness was typically proved by the fact that they can only decompile JDK 1.3 bytecode.
The only so-so working solution was to take the .class file and pre-process it, so it becomes JDK 1.3 compatible, and then run Jad over it (one of those older, but better decompilers).

But recently, a new wave of decompilers has forayed onto the market: Procyon, CFR, JD, Fernflower, Krakatau, Candle.
Here’s a list of decompilers presented on this site:

CFR

This free and open-source decompiler is available here: http://www.benf.org/other/cfr/
Author: Lee Benfield

  • Java 7: String switches
  • Java 8: lambdas
  • Java 9: modules
  • Java 11: dynamic constants
  • Java 12: Kotlin style «switch expressions»
  • Java 14: ‘instance of’ pattern match and ‘Record types’
JD

free for non-commercial use only, http://jd.benow.ca/
Author: Emmanuel Dupuy

Updated in 2015. Has its own visual interface and plugins to Eclipse and IntelliJ . Written in C++, so very fast. Supports Java 5.

Procyon
  • Enum declarations
  • Enum and String switch statements
  • Local classes (both anonymous and named)
  • Annotations
  • Java 8 Lambdas and method references (i.e., the :: operator).
Fernflower

Updated in 2015. Very promising analytical Java decompiler, now becomes an integral part of IntelliJ 14. (https://github.com/JetBrains/intellij-community/tree/master/plugins/java-decompiler)
Supports Java up to version 6 (Annotations, generics, enums)

JAD

given here only for historical reason. Free, no source-code available, jad download mirror
Author: Pavel Kouznetsov

Probably, this is the most popular Java decompiler, but primarily of this age only. Written in C++, so very fast.
Outdated, unsupported and does not decompile correctly Java 5 and later.

Источник

How to decompile a class file into a java file

How to decompile .class file

In this quick guide, you will learn how to decompile a .class file into a .java file. So let the decompilation begin.

Java is one of the most popular languages. It is cross-platform, robust and very popular in the industry. The cross-platform thing archived using a class file that is platform-independent. So instead of directly compiling its program code to machine-dependent code, It first compiles it to Bytecode, Which produces a .class file then that is compiled to machine-dependent code and gets executed in the JVM.

Decompiling a java .class file into a .java file

To convert the file, we need a decompiler. There are many options available to choose from but in this on we are going to use CFT decompiler. It is reliable and easy to use and supports almost all JDK versions.

You can download it using the link given below. This will download a .jar file which we will use for decompiling.

After downloading the decompiler, Now place the class file and compiler into a folder for the convenience and open up the terminal. You can also use your command prompt if you are on Windows and CD to the folder an run the below command :

java -jar cfr-0.14x.jar Class-file.class > Class-file.java

Above replace the Class-file with the actual file name and hit the enter key. This will generate a .java file, containing the actual code.

Alternatively, You can also use an online tool for the decompilation. Visit the given site for the same.

On this website. you can simply upload the .class file and it will do the job for you then you can download the converted .java file

Источник

Decompile Java Class file using decompilers.

duke-java-mascot

Byte codes generated by javac compiler can again be converted into java source. For this we need a decompiler tool. Decompilers are the utilities that generate the source code from input java class file. A Decompiler knows about the structure of a Java class and parse it to generated Java source code. Java decompilers will not give the exact Java source file from which class was generated and executed by Java Virtual Machine. But most of the structure will be maintained.

JAD: Java Decompiler

A lot of Decompilers are available in the market to decompile a class file. We will use one of the free decomipler called JAD. You can download JAD compiler from here . Update: JAD’s site is down hence you can download it from this link. I have used a simple Hello World java class to demonstrate JAD decompiler. Following is the code of HelloWorld.java file.

public class HelloWorld < public static void main(String args[]) < String fooBar = "Hello World from Java."; System.out.println(fooBar); > >
Code language: Java (java)
$> jad HelloWorld.class Parsing HelloWorld.class. Generating HelloWorld.jad
Code language: HTML, XML (xml)

The source file output of JAD is in file with .jad extension. You can change the extension to .java. Following is the source that you get after decompillng our HelloWorld.class file.

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov. // Jad home page: http://www.kpdus.com/jad.html // Decompiler options: packimports(3) // Source File Name: HelloWorld.java import java.io.PrintStream; public class HelloWorld < public HelloWorld() < >public static void main(String args[]) < String s = "Hello World from Java."; System.out.println(s); > >
Code language: Java (java)

Note that the string variable that we used in our original Java class was fooBar and it got changed into s. Any idea why so?

Источник

Оцените статью