Skip to content

Instantly share code, notes, and snippets.

@180254
Last active May 1, 2022 19:30
Show Gist options
  • Save 180254/5399d25b0b9be64ee18de54f08e46b14 to your computer and use it in GitHub Desktop.
Save 180254/5399d25b0b9be64ee18de54f08e46b14 to your computer and use it in GitHub Desktop.
java-decompiler
#!/bin/bash
# https://gist.github.com/180254/5399d25b0b9be64ee18de54f08e46b14
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <https://unlicense.org>
set -euo pipefail
[[ "${DEBUG:-0}" -eq 1 ]] && set -x
# default decompiler options:
# https://github.com/JetBrains/intellij-community/blob/master/plugins/java-decompiler/plugin/src/org/jetbrains/java/decompiler/IdeaDecompiler.kt#L55
if [[ "$#" -eq 0 ]]; then
echo "Usage: java-decompiler jarOrClass_file_path"
exit 1
fi
for FILE in "$@"; do
FILE_BASENAME="$(basename "${FILE}")"
IS_ZIP="false" && [[ "${FILE,,}" =~ \.(zip|jar)$ ]] && IS_ZIP="true"
DESTINATION="."
if [[ "$IS_ZIP" == "true" ]]; then
DESTINATION="./${FILE_BASENAME}_sources"
mkdir -p "${DESTINATION}"
fi
INTELLIJ_DIRS=("/snap/intellij-idea-ultimate/current/" "${HOME}/.local/share/JetBrains/")
JAVA_DECOMPILER_JAR=""
for INTELLIJ_DIR in "${INTELLIJ_DIRS[@]}"; do
if [ ! -d "${INTELLIJ_DIR}" ]; then
continue
fi
JAVA_DECOMPILER_JAR=$(find "${INTELLIJ_DIR}" -name java-decompiler.jar -type f -print -quit)
if [[ "${JAVA_DECOMPILER_JAR}" != "" ]]; then
break
fi
done
if [[ "${JAVA_DECOMPILER_JAR}" == "" ]]; then
echo "err: unable to find IntelliJ IDEA and/or java-decompiler.jar (IntelliJ IDEA should include it)"
exit 1
fi
BANNER=$'//\n// Source code recreated from .class file by org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler \n// (part of IntelliJ IDEA; powered by FernFlower decompiler)\n//\n\n'
java -cp "$JAVA_DECOMPILER_JAR" \
org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler \
-hdc=0 -dgs=1 -rsy=1 -rbr=1 -nls=1 -ban="${BANNER}" -mpm=60 -ind=" " -iib=1 -vac=1 \
"${FILE}" "${DESTINATION}"
if [[ "$IS_ZIP" == "true" ]]; then
pushd "${DESTINATION}"
unzip "${FILE_BASENAME}"
popd # was "${DESTINATION}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment