summaryrefslogtreecommitdiff
path: root/show-jar-java-version
blob: 5acf8f7628a2ad10a7c6beffd2b5a664bc0c4e63 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

#!/bin/sh
#
#  show-jar-java-version -- Determines the target Java version of a JAR file
#
#  Copyright (c) 2014 Samuel Lidén Borell <samuel@kodafritt.se>
#  
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#  
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#  
#  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 OR COPYRIGHT HOLDERS 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.
#

ignore_manifest=0

find_in_manifest() {
    createdby=""
    target=""
    IFS=": "
    while read header value; do
        if [ "x$header" = "xCreated-By" ]; then
            createdby="$value"
        elif [ "x$header" = "xX-Compile-Target-JDK" ]; then
            target="$value"
        fi
    done
    
    if [ -n "$target" ]; then
        echo "$target"
    elif [ -n "$createdby" ]; then
        echo "$createdby" | grep -oE '^([0-9]+\.[0-9]+)'
    fi
}

determine_version() {
    [ ! -r "$1" ] && { echo "failed to open file"; return 1; }
    
    if [ "$ignore_manifest" = 0 ]; then
        manifestversion="`unzip -p -- "$1" META-INF/MANIFEST.MF | find_in_manifest`"
        if [ -n "$manifestversion" ]; then
            echo "$manifestversion"
            return 0
        fi
    fi
    
    someclass="`jar tf "$1" | grep -E '\.class$' | head -n 1`"
    if [ -z "$someclass" ]; then
        echo "empty archive";
        return 0
    fi
    
    #unzip -p -- "$1" "$some_class" | head -c 8 | tail -c 2 | hd
    majorversion="`unzip -p -- "$1" "$someclass" | hexdump -v -e '/1 "%02X" "%02X\n"' -s 6 -n 2`"
    case "$majorversion" in
        002D) echo "1.1";;
        002E) echo "1.2";;
        002F) echo "1.3";;
        0030) echo "1.4";;
        0031) echo "1.5";;
        0032) echo "1.6";;
        0033) echo "1.7";;
        0034) echo "1.8";;
        *) echo "unknown java major version $majorversion";;
    esac
    return 0
}

parse_opts() {
    parsing_opts=1
    ignoremanifest=0
    error=0
    while [ $# != 0 ]; do
        if [ $parsing_opts = 1 ]; then
            if [ "x$1" = "x--" ]; then
                parsing_opts=0
                shift
                continue
            elif [ "x$1" = "x--ignore-manifest" ]; then
                ignoremanifest=1
                shift
                continue
            elif [ "x${1#-}" != "x$1" ]; then
                echo "$0: unsupported option: $1" >&2
                error=1
            fi
        fi
        shift
    done
    if [ $error = 1 ]; then
        echo ""
    else
        echo "$ignoremanifest"
    fi
}

if [ $# = 0 ]; then
    echo "usage: $0 [--ignore-manifest] JARFILES..."
    exit 1
elif [ $# = 1 ]; then
    determine_version "$1"
else
    ignore_manifest="`parse_opts "$@"`"
    parsing_opts=1
    [ -z "$ignore_manifest" ] && exit 1
    while [ $# != 0 ]; do
        if [ $parsing_opts = 1 ]; then
            if [ "x$1" = "x--" ]; then
                parsing_opts=0
                shift
                continue
            elif [ "x${1#-}" != "x$1" ]; then
                shift
                continue
            fi
        fi
        printf "%s: " "$1"
        determine_version "$1"
        shift
    done
fi