forked from scijava/scijava-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-version.sh
More file actions
executable file
·59 lines (52 loc) · 1.21 KB
/
Copy pathclass-version.sh
File metadata and controls
executable file
·59 lines (52 loc) · 1.21 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
#!/bin/sh
# class-version.sh - find the Java version which wrote a JAR file
for jar in "$@"
do
# find the first class of the JAR
class="$(jar tf "$jar" | grep '\.class' | head -n 1 | sed 's/\//./g' | sed 's/\.class$//')"
if [ -z "$class" ]
then
echo "$jar: No classes"
continue
fi
# extract bytes 4-7
info="$(unzip -p "$jar" "$(jar tf "$jar" | grep \.class$ | head -n 1)" | head -c 8 | hexdump -s 4 -e '4/1 "%d\n" "\n"')"
minor1="$(echo "$info" | sed -n 1p)"
minor2="$(echo "$info" | sed -n 2p)"
major1="$(echo "$info" | sed -n 3p)"
major2="$(echo "$info" | sed -n 4p)"
# compute major.minor version
minor="$(expr 256 \* $minor1 + $minor2)"
major="$(expr 256 \* $major1 + $major2)"
# derive Java version
case $major in
45)
version="JDK 1.1"
;;
46)
version="JDK 1.2"
;;
47)
version="JDK 1.3"
;;
48)
version="JDK 1.4"
;;
49)
version="J2SE 5.0"
;;
50)
version="J2SE 6.0"
;;
*)
if [ "$major" -gt 50 ]
then
version="J2SE $(expr $major - 44)"
else
version="Unknown"
fi
;;
esac
# report the results
echo "$jar: $version ($major.$minor)"
done