Java code from class file

How to see java code from class file

But there are many complexities, for example, Java code is structured, and byte code is not structured. There is a risk of overwriting of source code files, especially when the ‘-o’ (overwrite output files without confirmation) parameter is used.

How do I «decompile» Java class files?

www.javadecompilers.com lists JAD as being:

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

So your mileage may vary with recent jdk (7, 8).

The same site list other tools.

And javadecompiler, as noted by Salvador Valencia in the comments (Sept 2017), offers a SaaS where you upload the .class file to the cloud and it returns you the decompiled code.

  • The final release of JSR 176, defining the major features of J2SE 5.0 (Java SE 5), has been published on September 30, 2004.
  • The lastest Java version supported by JAD, the famous Java decompiler written by Mr. Pavel Kouznetsov, is JDK 1.3 .
  • Most of the Java decompilers downloadable today from the Internet, such as “DJ Java Decompiler” or “Cavaj Java Decompiler”, are powered by JAD: they can not display Java 5 sources.
Читайте также:  Php ip address сервера

Java Decompiler (Yet another Fast Java decompiler) has:

  • Explicit support for decompiling and analyzing Java 5+ “.class” files.
  • A nice GUI:

screenshot

It works with compilers from JDK 1.1.8 up to JDK 1.7.0, and others (Jikes, JRockit, etc.).

It features an online live demo version that is actually fully functional! You can just drop a jar file on the page and see the decompiled source code without installing anything.

There are a few decompilers out there. A quick search yields:

  1. Procyon: open-source (Apache 2) and actively developed
  2. Krakatau: open-source (GPLv3) and actively developed
  3. CFR: open-source (MIT) and actively developed
  4. JAD
  5. DJ Java Decompiler
  6. Mocha

These produce Java code. Java comes with something that lets you see JVM byte code (javap).

To see Java source code check some decompiler. Go search for jad .

If you want to see bytecodes, just use javap which comes with the JDK.

Analyzing the code of the class files in a jar file, Class files are compiled files, which means the only way to read the code inside them would be to decompile them. class files contain byte

How to Convert Class File to Java File

BytecodetoSourcecode #ClassFiletoJavaFile #RK_KeynotesIn this video, I have shown how to Duration: 2:19

How to decompile .jar file or .class file?

How to decompile .jar file or .class file? | Java Decompilers & eclipse plugin for decompile. 43 Duration: 11:15

Viewing source code of Java’s built-in classes with Eclipse

How can I open Java .class files in a human-readable way?

jd-gui is the best decompiler at the moment. it can handle newer features in Java, as compared to the getting-dusty JAD.

If you don’t mind reading bytecode, javap should work fine. It’s part of the standard JDK installation.

Usage: javap . where options include: -c Disassemble the code -classpath Specify where to find user class files -extdirs Override location of installed extensions -help Print this usage message -J Pass directly to the runtime system -l Print line number and local variable tables -public Show only public classes and members -protected Show protected/public classes and members -package Show package/protected/public classes and members (default) -private Show all classes and members -s Print internal type signatures -bootclasspath Override location of class files loaded by the bootstrap class loader -verbose Print stack size, number of locals and args for methods If verifying, print reasons for failure 

As pointed out by @MichaelMyers, use

to get the JVM assembly code. You may also redirect the output to a text file for better visibility.

How to decompile .jar file or .class file?, How to decompile .jar file or .class file? | Java Decompilers & eclipse plugin for decompile. 43 Duration: 11:15

Java Decompiler

A Java Decompiler is a special type of decompiler which takes a class file as input and produces Java source code as output. The decompilation is exactly the reverse process of compilation. Thus, decompiler does not produce a replica of the source code. It is because a bit of information lost from the source code during the compilation of the program.

But there are many complexities, for example, Java code is structured, and byte code is not structured. Moreover, the transformation is not one to one: two different Java programs may yield identical byte code.

There are following Java decompilers available:

JAD Decompiler

The JAD is a command-line application. It is written in C++. The main function is to disassemble the .class files. JAD can be deployed from the console, and it will decompile Java classes into the source code. This way, developers can look deep into the code of Java runtime libraries and even beautify the lines, preparing them for a recompilation. There is a risk of overwriting of source code files, especially when the ‘-o’ (overwrite output files without confirmation) parameter is used.

Let’s create a simple Java program which adds two number.

import java.util.*; class DecompilerDemo < public static void main(String args[]) < int a,b,c; Scanner sc=new Scanner(System.in); System.out.print("Enter the value of a: "); a=sc.nextInt(); System.out.print("Enter the value of b: "); b=sc.nextInt(); c=a+b; System.out.println("sum of a and b is: "+c); >>

Follow the steps to convert the .class into readable Java source code. Here we are using JAD decompiler.

Step 1: Download JAD tool from https://bit.ly/2XuRzT5

Step 2: Extract the zip file and get jad.exe.

Java Decompiler

Step 3: Create a .java file (DecompilerDemo.java) in the same folder in which you have extracted the zip file.

Step 4: Compile and run the .java file. It will generate a .class file in the respective folder.

Java Decompiler2

Step 5: Now move the .class file and jad application to another folder.

Java Decompiler3

Step 6: Type the command: jad DecompilerDemo

Java Decompiler4

Above command generates a .jad file in the respective folder.

Java Decompiler5

Step 7: Type the command: jad ?s java DecompilerDemo

Java Decompiler6

It generates the .java file in the same folder.

Java Decompiler7

Open the DecompilerDemo.java we get the following code:

// 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: DecompilerDemo.java import java.io.PrintStream; import java.util.Scanner; class DecompilerDemo < DecompilerDemo() < >public static void main(String args[]) < Scanner scanner = new Scanner(System.in); System.out.print("Enter the value of a: "); int i = scanner.nextInt(); System.out.print("Enter the value of b: "); int j = scanner.nextInt(); int k = i + j; System.out.println((new StringBuilder()).append("sum of a and b is: ").append(k).toString()); >>

We can observe that written .java file is different from the generated .java file but gives the same output.

Java Class File Disassembler

javap Command

The javap is a Java command line tool which comes with JDK. It is used to disassemble the class file and prints a human-readable version of that class. Its output depends on the option used. Without using any option, javap prints the package, protected and public fields and methods of the classes passed to it. A javap is also used to find out which methods are available for a class if you don?t have the source code that was used to create the class. The general format of javap command is:

With javap command, many options are available. Some of them are:

  • -c: It prints disassembled code.
  • -p: It shows all classes and members.
  • -s: It prints internal type signatures.
  • -constants: It shows static final constants.

Disassemble a class file using javap command

Generate the class file of the above program and use the following command to disassemble the class file.

javap -c -p DisassembleDemo

The following output shows what the class file has inside.

Java Decompiler8

But we do not get the Java source code by using the javap command. So there are many decompilers available which convert a .class file into a .java file.

Java Class File, java files as a result of successful compilation. As we know that a single Java programming language source file (or we can say .java file) may

How to get source code from .class file( class file version is 1.2)

Try this at the command line. It will also show the JDK version used to compile the class, in the beginning , like: major version: 52

Try javadecompilers.com, I’ve successfully decompiled some class files there.

How to open *.class files with decompiler by default in Eclipse?, At the moment *.class files are consistently opening in the Class File Viewer editor regardless of my settings. I have to go back to the Package

Источник

Java Class File

A Java class file is a file containing Java bytecode and having .class extension that can be executed by JVM. A Java class file is created by a Java compiler from .java files as a result of successful compilation. As we know that a single Java programming language source file (or we can say .java file) may contain one class or more than one class. So if a .java file has more than one class then each class will compile into a separate class files.
For Example: Save this below code as Test.java on your system.

For Compiling:

After compilation there will be 3 class files in corresponding folder named as:

A single class file structure contains attributes that describe a class file.
Representation of Class File Structure

Elements of class file are as follows:

  1. magic_number: The first 4 bytes of class file are termed as magic_number. This is a predefined value which the JVM use to identify whether the .class file is generated by valid compiler or not. The predefined value will be in hexadecimal form i.e. 0xCAFEBABE.
    Now let’s see what happen when JVM will not find valid magic number. Suppose we have a .java file named as Sample.java as follows and follow step by step process on your system.
// class Declaration class Sample < public static void main(String[] args) < System.out.println("Magic Number"); >>

Step 1: Compile using javac Sample.java
Step 2: Now open the Sample.class file. It will looks like following.

Sample.class File

Runtime Exception Due to Invalid magic number

Step 3: Now erase at least single symbol from this Sample.class file from starting of file and save it.
Step 4: Now try to run this using java Sample command and see the magic i.e. you will get run time exception (See the highlighted text in below image):

Note: This can vary depending on how much you remove the .class file data.

Note: Lower version compiler generated .class file can be executed by high version JVM but higher version compiler generated .class file cannot be executed by lower version JVM. If we will try to execute we will get run time exception.

This demonstration is for Windows OS as follows:

Step 1: Open a command prompt window and try to check java compiler version and JVM version using following commands respectively (Highlighted text in image are the commands)
Output for 1.8 version will be:
Java Compiler Version
JVM Version

Step 2: Now check with another version which may be higher or lower than already installed.thisDownload link.
And install this to your PC or laptops and note the installation address.
Step 3: Open a second command prompt window and set the path of bin folder of installed jdk installed during 2nd step. And check for Java compiler version ad JVM version.
Checking Version of JDK 1.6
Step 4: Now on 1st command prompt compile the any valid .java file. For example: See above Sample.java file. Compile it as:
Compiling with Compiler Version 1.8
Step 5: Now on 2nd command prompt window try to run the above compiled code class file and see what happen. There is a run time exception which I have highlighted in below image.

Runtime Exception Due to Invalid major and minor Version of class file

Note: Internally jdk 1.5 version means 49.0 and 1.6 means 50.0 and 1.7 means 51.0 etc. class file version where the digits before the decimal point represent the major_version and digits after decimal point represents the minor_version.

  1. constant_pool_count: It represents the number of the constants present in the constant pool (When a Java file is compiled, all references to variables and methods are stored in the class’s constant pool as a symbolic reference).
  2. constant_pool[]: It represents the information about constants present in constant pool file.
  3. access_flags: It provide the information about the modifiers which are declared to the class file.
  4. this_class: It represents fully qualified name of the class file.
  5. super_class: It represents fully qualified name of the immediate super class of current class. Consider above Sample.java file. When we will compile it, then we can say this_class will be Sample class and super_class will be Object class.
  6. interface_count: It returns the number of interfaces implemented by current class file.
  7. interface[]: It returns interfaces information implemented by current class file.
  8. fields_count: It represents the number of fields (static variable) present in current class file.
  9. fields[]: It represent fields (static variable) information present in current class file.
  10. method_count: It represents number of methods present in current class file.
  11. method[]: It returns information about all methods present in current class file.
  12. attributes_count: It returns the number of attributes (instance variables) present in current class file.
  13. attributes[]: It provides information about all attributes present in current class file.

Источник

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