#!/bin/sh # # show-jar-java-version -- Determines the target Java version of a JAR file # # Copyright (c) 2014 Samuel Lidén Borell # # 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