Extract Source Code from .Jar File

How to get source code from .jar file

First of all I would like to tell you that if your project is big and complex, you are in trouble. Generated source code via external tools(no matter whatever the tool is) is never same as the real code. Code like comments, constants, inner classes, etc gets messy.

For simpler code and projects you can use -

  • Java Decompiler (JD-GUI) http://jd.benow.ca/
  • DJ Java Decompiler http://www.neshkov.com/dj.html

But always know this that its not what was written originally in the source code.

How to view the source code inside a JAR file?

Sourceforge has links to the source.

How to extract the source code from a *.jar file on a Mac?

  • Download FernFlower.jar: https://the.bytecode.club/fernflower.jar //the fernflower.jar site changed from http to https
  • Documentation: http://the.bytecode.club/fernflower.txt
  • Repository: https://github.com/JetBrains/intellij-community/tree/master/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler

Run "java -jar fernflower.jar -dgs=true JarToDecompile.jar DecompiledJar"

This is what Intelli-J & Android-Studio Decompiler does.

Note: Fernflower extracts the .java files to a .jar file. You can either Unzip the jar file as a regular zip file (if your version of Archive Utility on OSX allows it -- It doesn't do it for me on OSX Sierra but works on El Capitan) OR you can do jar xf DecompiledJar and it'll extract it.

Example (all in one command -- multiple commands separated by &&):

java -jar fernflower.jar -dgs=true JarToDecompile.jar DecompiledJar && cd DecompiledJar && jar xf DecompiledJar.jar && cd ../

How do I extract source code from a Jar file?

I've received source code for a Java product ...

If you have really received the source code of a product, and all you've got is JAR files, then the JAR files (which are actually ZIP files with a different file suffix and a particular kind of "manifest") should contain a bunch of files with the file suffix ".java". You should be able to check this using any ZIP archive tool.

If there are no ".java" files in the JARs (e.g. only a lot of ".class" and other files), you do not have the source code for the product. Making changes will be really really hard, given that you are not a Java developer.

Assuming that you are doing this legitimately (i.e. with the explicit or implicit permission of the product's developer) you will save yourself a lot of time if you can also get hold of the product's build instructions. For example, if it is built using Ant, you want the "build.xml" file(s).

Given an existing jar file and source code, how do I alter the code?

Found this code here to extract the JAR contents:

import java.io.*;
import java.util.*;
import java.util.jar.*;
import java.util.zip.ZipException;

public class jara {
public static void main (String args[])throws IOException,ZipException
{
JarFile jarFile = new JarFile("jarfile.jar");
Enumeration en = jarFile.entries();
while (en.hasMoreElements()) {
String ent=proc(en.nextElement());
if(ent.indexOf("/")>0)
{
String fil=ent.substring(0,ent.indexOf("/"));
System.out.println(fil);
File local=new File(fil);
if(!local.exists())
local.mkdirs();
}
if(ent.indexOf(".")>0)
{
int n=ent.length();
String fil1=ent.substring(ent.lastIndexOf("/")+1,n);
System.out.println(fil1);
extract(jarFile.getName(),ent);
}

}
}

public static String proc(Object obj)
{
JarEntry entry = (JarEntry)obj;
String name = entry.getName();
System.out.println("\nEntry Name: "+name);
return(name);
}

public static void extract(String jarName,String entryName)throws IOException,ZipException
{
JarFile jar = new JarFile(jarName);
System.out.println(jarName + " opened.");

try {
// Get the entry and its input stream.

JarEntry entry = jar.getJarEntry(entryName);

// If the entry is not null, extract it. Otherwise, print a
// message.

if (entry != null) {
// Get an input stream for the entry.

InputStream entryStream = jar.getInputStream(entry);

try {
// Create the output file (clobbering the file if it exists).

FileOutputStream file = new FileOutputStream(entry.getName());

try {
// Allocate a buffer for reading the entry data.

byte[] buffer = new byte[1024];
int bytesRead;

// Read the entry data and write it to the output file.

while ((bytesRead = entryStream.read(buffer)) != -1) {
file.write(buffer, 0, bytesRead);
}

System.out.println(entry.getName() + " extracted.");
}
finally {
file.close();
}
}
finally {
entryStream.close();
}
}
else {
System.out.println(entryName + " not found.");
} // end if
}
finally {
jar.close();
System.out.println(jarName + " closed.");
}
}
}

Then, just read the Java source files as if they were text files, modify them, then write them to new files.

Then to package the jar, you can use this tutorial, which uses the Java Compiler API.

How can I retrieve my Java source code from a JAR file?

You could try "decompiling" your JAR file using any JAR decompiler, such as http://www.javadecompilers.com/.

You won't get exactly the source code you originally wrote, but the decompiler will do it's best to map the .class files embedded in the JAR back to Java source code.

Read the source code of .jar files with python

Do you require a Python library specifically? Krakatau is a command line tool in Python for decompiling .jar files, you can perhaps import it and use the relevant functions from inside your script.

Alternatively, you can call it, or any other command line .jar decompiler such as Procyon,
using Python's Subprocess.

In the 2nd case, you would most likely like to redirect and capture stdout and/or stderr. A basic call may look something like:

import os    
from subprocess import Popen, PIPE
.
.
jar_decompiler_output = Popen(('jar_decompiler', '1stparam', '2ndparam',..), stdout= PIPE).communicate()[0].split(os.linesep)

Note that communicate() returns a tuple.

Converting Jar File into Eclipse Project

You can follow the steps given below:

  1. Make sure you have a working decompiler in eclipse. If not, please install https://marketplace.eclipse.org/content/enhanced-class-decompiler into your eclipse.
  2. Create a new Java project in eclipse > Right-click the src folder > Click import > Select Archive from the list to import your JAR
  3. Expand the JAR in eclipse and double click a class file to decompile it.

    Sample Image



Related Topics



Leave a reply



Submit