forked from HariSekhon/DevOps-Bash-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_decompile_jar.sh
More file actions
executable file
·98 lines (77 loc) · 2.13 KB
/
java_decompile_jar.sh
File metadata and controls
executable file
·98 lines (77 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env bash
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2024-08-28 21:07:22 +0200 (Wed, 28 Aug 2024)
#
# https///github.com/HariSekhon/DevOps-Bash-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
srcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC1090,SC1091
. "$srcdir/lib/utils.sh"
# shellcheck disable=SC2034,SC2154
usage_description="
Decompiles a Java jar
Unpacks it in /tmp, finds the Main-Class from the META-INF/MANIFEST.MF
and runs the Java decompiler JD GUI on its main .class file
Uses adjacent script ./jd_gui.sh
which uses ../install/download_jd_gui_jar.sh if the JD GUI jar is not already available
Require Java to already be installed and in the \$PATH
"
# used by usage() in lib/utils.sh
# shellcheck disable=SC2034
usage_args="<jar>"
help_usage "$@"
min_args 1 "$@"
jar="$1"
readlink(){
if is_mac; then
command greadlink "$@"
else
command readlink "$@"
fi
}
if ! [[ "$jar" =~ \.jar$ ]]; then
die "ERROR: arg given is not a JAR file: $jar"
fi
if ! [[ -f "$jar" ]]; then
die "ERROR: jar file not found: $jar"
fi
jar="$(readlink -f "$jar")"
if is_mac; then
jar="${jar/\/private\/tmp/\/tmp}"
fi
if [ "$jar" != "/tmp/${jar##*/}" ]; then
timestamp "Copying JAR to /tmp:"
echo >&2
unalias cp >&/dev/null || :
cp -fv "$jar" /tmp/
echo >&2
fi
cd /tmp/
jar="${jar##*/}"
timestamp "Unpacking JAR"
#echo >&2
tar zxf "$jar"
#echo >&2
echo >&2
timestamp "Determining main class"
main_class="$(awk '/Main-Class:/{print $2}' META-INF/MANIFEST.MF)"
echo >&2
if is_blank "$main_class"; then
die "ERROR: failed to find Main-Class from unpacked jar's META-INF/MANIFEST.MF"
fi
timestamp "Determined main class to be: $main_class"
echo >&2
main_class_file="${main_class//./\/}"
timestamp "Running decompiler on main class: $main_class_file"
echo >&2
"$srcdir/jd_gui.sh" "$main_class_file"