Skip to content

Instantly share code, notes, and snippets.

@cspinetta
Last active June 4, 2021 17:50
Show Gist options
  • Save cspinetta/f88007249bd54092094443c5cd8dd1eb to your computer and use it in GitHub Desktop.
Save cspinetta/f88007249bd54092094443c5cd8dd1eb to your computer and use it in GitHub Desktop.
An script to decompile multiple java classes with CFR Java Decompiler: http://www.benf.org/other/cfr/
#!/usr/bin/env bash
# USAGE: ./decompile-java-classes.sh -d {cfr jar} -i {input directory} -n {fiel name pattern} -o {output directory}
# Output directory and file name is optional
# File name accepts wildcard such as '*'
OUTPUTDIR="/tmp/decompiled-classes/"
FILE_NAME="*"
SCRIPT_USAGE="USAGE: ./decompile-java-classes.sh -d {cfr jar} -i {input directory} -n {fiel name pattern} -o {output directory}"
export PROCESSED_FILES=0
while getopts "d:i:o:n:" opt; do
case "$opt" in
d)
DECOMPILER=$OPTARG
;;
i)
INPUTDIR=$OPTARG
;;
n)
FILE_NAME=$OPTARG
;;
o)
OUTPUTDIR=$OPTARG
esac
done
function decompileFile() {
decompiler="$1"
file=$2
outputdir=$3
java -jar ${decompiler} ${file} --outputdir ${outputdir}
returnValue=$?
if [ $returnValue -eq 0 ] ; then
echo -n "."
else
return $returnValue
fi
}
export -f decompileFile
if [ -z "$INPUTDIR" ] ;
then
echo "No input directory supplied";
echo $SCRIPT_USAGE;
exit 1;
elif [ -z "$DECOMPILER" ] ; then
echo "No decompiler supplied";
echo $SCRIPT_USAGE;
exit 1;
else
FILES_COUNT=$(find ${INPUTDIR} -type f -name "${FILE_NAME}.class" | wc -l)
echo "There are $FILES_COUNT files to process"
find ${INPUTDIR} -type f -name "${FILE_NAME}.class" \
-exec bash -c 'decompileFile "$0" "$1" "$2"' ${DECOMPILER} {} ${OUTPUTDIR} \;
returnValueFind=$?
if [ $returnValueFind -eq 0 ] ; then
echo -ne "\nFiles successfully decompiled in '${OUTPUTDIR}'\n\n"
else
echo -ne "\nCFR Decompile failed =(\n\n"
fi
exit $returnValueFind;
fi
@damdev
Copy link

damdev commented Apr 3, 2016

Cris fijate que en el exit $? vas a tener siempre un 0, ya que te va a devolver el resultado de la ultima ejecucion, en este caso el echo por OK o por error

@cspinetta
Copy link
Author

Gracias por el comentario Dami!

@cspinetta
Copy link
Author

Como tarda mucho tiempo cuando procesas muchos archivos, le agregué algunas boludeces que te indica que está procesando, y no que se colgó en alguna decompilación.

@dylanjacob
Copy link

👍

@cspinetta
Copy link
Author

cspinetta commented Apr 27, 2016

Minor changes to see on the console the accepted parameters.

@o-nix
Copy link

o-nix commented Jun 4, 2021

find . -type f -name "*.class" -exec bash -c 'java -jar cfr-0.151.jar $0 --outputdir decompiled' {} \;
🤷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment