Suriyaa
Suriyaa

Reputation: 2296

How to read a class file from an extracted jar file?

I want to read a .class file that is located inside of a .jar package. How can I read a readable .class file from a .jar package?

My environment is:


EDIT:

My extracted .class file that contains binary & compiled bytecode:

The .class file contains binary & compiled bytecode

The output I want:

The .java file - readable code

Upvotes: 5

Views: 24003

Answers (2)

Aahash
Aahash

Reputation: 71

Extract the Contents of .zip/.jar files programmatically

Suppose .jar file is the .jar/.zip file to be extracted. destDir is the path where it will be extracted:

java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);
java.util.Enumeration enum = jar.entries();
while (enum.hasMoreElements()) {
    java.util.jar.JarEntry file = (java.util.jar.JarEntry) enum.nextElement();
    java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());
    if (file.isDirectory()) { // if its a directory, create it
        f.mkdir();
        continue;
    }
    java.io.InputStream is = jar.getInputStream(file); // get the input stream
    java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
    while (is.available() > 0) {  // write contents of 'is' to 'fos'
        fos.write(is.read());
    }
    fos.close();
    is.close();
}

Upvotes: 5

RoccoDev
RoccoDev

Reputation: 526

Use a decompiler. I prefer using Fernflower, or if you use IntelliJ IDEA, simply open .class files from there, because it has Fernflower pre-installed.

Or, go to javadecompilers.com, upload your .jar file, use CFR and download the decompiled .zip file.

However, in some cases, decompiling code is quite illegal, so, prefer to learn instead of decompiling.

Upvotes: 17

Related Questions