[java] How to decompile a whole Jar file?

Does anyone know of a free decompiler that can decompile an entire Jar file instead of a single class? I have a problem with sub classes like name$1.class name$2.class name.class

This question is related to java jar decompiler

The answer is


I have had reasonable success with a tool named (frustratingly) "JD: Java Decompiler".

I have found it better than most decompilers when dealing with classes compiled for Java 5 and higher. Unfortunately, it can still have some hiccups where JAD would normally succeed.


First of all, it's worth remembering that all Java archive files (.jar/.war/etc...) are all basically just fancy.zip files, with a few added manifests and metadata.

Second, to tackle this problem I personally use several tools which handle this problem on all levels:

  • Jad + Jadclipse while working in IDE for decompiling .class files
  • WinRAR, my favorite compression tool natively supports Java archives (again, see first paragraph).
  • Beyond Compare, my favorite diff tool, when configured correctly can do on-the-fly comparisons between any archive file, including jars. Well worth a try.

The advantage of all the aforementioned, is that I do not need to hold any other external tool which clutters my work environment. Everything I will ever need from one of those files can be handled inside my IDE or diffed with other files natively.


Something like:

jar -xf foo.jar && find . -iname "*.class" | xargs /opt/local/bin/jad -r

maybe?


Note: This solution only works for Mac and *nix users.

I also tried to find Jad with no luck. My quick solution was to download MacJad that contains jad. Once you downloaded it you can find jad in [where-you-downloaded-macjad]/MacJAD/Contents/Resources/jad.


If you happen to have both a bash shell and jad:

JAR=(your jar file name)
unzip -d $JAR.tmp $JAR
pushd $JAR.tmp
for f in `find . -name '*.class'`; do
    jad -d $(dirname $f) -s java -lnc $f
done
popd

I might be a tiny, tiny bit off with that, but it should work more or less as advertised. You should end up with $JAR.tmp containing your decompiled files.


Insert the following into decompile.jar.sh

# Usage: decompile.jar.sh some.jar [-d]

# clean target folders
function clean_target {
  rm -rf $unjar $src $jad_log
}

# clean all debug stuff
function clean_stuff {
  rm -rf $unjar $jad_log
}

# the main function
function work {
  jar=$1
  unjar=`basename $jar.unjar`
  src=`basename $jar.src`
  jad_log=jad.log

  clean_target

  unzip -q $jar -d $unjar
  jad -d $src -ff -r -lnc -o -s java $unjar/**/*.class > $jad_log 2>&1

  if [ ! $debug ]; then
    clean_stuff
  fi

  if [ -d $src ] 
    then
      echo "$jar has been decompiled to $src"
    else
      echo "Got some problems check output or run in debug mode"
  fi
}

function usage {
  echo "This script extract and decompile JAR file"
  echo "Usage: $0 some.jar [-d]"
  echo "    where: some.jar is the target to decompile"
  echo "    use -d for debug mode"
}

# check params
if [ -n "$1" ]
  then
    if [ "$2" == "-d" ]; then
      debug=true
      set -x
    fi
    work $1
  else
    usage
fi
  • chmod +x decomplie.jar.sh //executable
  • ln -s ./decomplie.jar.s /usr/bin/dj

Ready to use, just type dj your.jar and you will get your.jar.src folder with sources. Use -d option for debug mode.


You extract it and then use jad against the dir.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to jar

Running JAR file on Windows 10 Add jars to a Spark Job - spark-submit Deploying Maven project throws java.util.zip.ZipException: invalid LOC header (bad signature) java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory Maven Error: Could not find or load main class Where can I download mysql jdbc jar from? Access restriction: The type 'Application' is not API (restriction on required library rt.jar) Create aar file in Android Studio Checking Maven Version Creating runnable JAR with Gradle

Examples related to decompiler

Android - How to decode and decompile any APK file? How to change already compiled .class file without decompile? Decompile Python 2.7 .pyc how to use DEXtoJar How to get Java Decompiler / JD / JD-Eclipse running in Eclipse Helios decompiling DEX into Java sourcecode How to decompile a whole Jar file? How do I "decompile" Java class files?