0


【JD-GUI】MacOS 中使用Java反编译工具JD-GUI

希望文章能给到你启发和灵感~
如果觉得文章对你有帮助的话,点赞 + 关注+ 收藏 支持一下博主吧~

阅读指南

开篇说明

现在的反编译软件繁多,Apktool,JD-GUI,dedexer,IDA Pro等等,每种反编译工具都有各自使用的场景,例如Apktool我们在反编译apk类型文件时会用到,JD-GUI则是常用来反编译Java中的class文件,或者Jar等源文件;

在这里插入图片描述

概念理解

【1】什么是反编译?

简单的说,就是将机器语言转换成我们能看懂的语言,这个过程就叫反编译,例如Java中的class文件反编译后,就能变成我们日常开发写的代码xxx.java文件;

【2】反编译是否一定能成功?

不一定,随着现在很多代码编译中都会加入代码混淆,加密,或是加壳等等技术,加大反编译的难度和降低反编译代码的可读性,不过如果想完全避免反编译也是不可能的。只是反编译的还原度的大小问题了;

一、基础环境说明

考虑环境因素,大家适当的对比自己的软硬件环境情况分析~请仔细阅读硬件、软件环境

1.1 硬件环境

  1. MacOS Monterey 版本 12.6.8 Apple M1

1.2 软件环境

JD-GUI版本:

  1. jd-gui-osx-1.6.6

二、下载与安装

Github下载JD-GUI软件github java-decompiler/jd-gui Public

2.1 选择对应版本

macOS系统我们选择

  1. jd-gui-osx-1.6.6.tar

压缩包即可;

在这里插入图片描述

2.2 解压运行排除异常:

解压后,进入文件打开JD-GUI;
在这里插入图片描述
【注】运行时,

  1. 需要注意自己环境已经安装了JDK

,否则你可能会遇到这样的错误:

在这里插入图片描述
若是确认JDK安装后,仍然提示该错误,需要修改文件->

  1. 显示包内容->Contents->MacOs->universalJavaApplicationStub.sh;

在这里插入图片描述
将内容替换成下面的内容:

  1. #!/bin/bash################################################################################### ## universalJavaApplicationStub ## ## A BASH based JavaApplicationStub for Java Apps on Mac OS X ## that works with both Apple's and Oracle's plist format. ## ## Inspired by Ian Roberts stackoverflow answer ## at http://stackoverflow.com/a/17546508/1128689 ## ## @author Tobias Fischer ## @url https://github.com/tofi86/universalJavaApplicationStub ## @date 2020-03-19 ## @version 3.0.6 ## #################################################################################### ## The MIT License (MIT) ## ## Copyright (c) 2014-2020 Tobias Fischer ## ## 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. ## #################################################################################### function 'stub_logger()'## A logger which logs to the macOS Console.app using the 'syslog' command## @param1 the log message# @return void################################################################################functionstub_logger(){
  2. syslog -s-k\
  3. Facility com.apple.console \
  4. Level Notice \
  5. Sender "$(basename"$0")"\
  6. Message "[$$][${CFBundleName:-$(basename "$0")}] $1"}# set the directory abspath of the current# shell script with symlinks being resolved############################################PRG=$0while[-h"$PRG"];dols=$(ls-ld"$PRG")link=$(expr"$ls":'^.*-> \(.*\)$'2>/dev/null)ifexpr"$link":'^/'2> /dev/null >/dev/null;thenPRG="$link"elsePRG="$(dirname"$PRG")/$link"fidonePROGDIR=$(dirname"$PRG")
  7. stub_logger "[StubDir] $PROGDIR"# set files and folders############################################# the absolute path of the app packagecd"$PROGDIR"/../../ ||exit11AppPackageFolder=$(pwd)# the base path of the app packagecd..||exit12AppPackageRoot=$(pwd)# set Apple's Java folderAppleJavaFolder="${AppPackageFolder}"/Contents/Resources/Java
  8. # set Apple's Resources folderAppleResourcesFolder="${AppPackageFolder}"/Contents/Resources
  9. # set Oracle's Java folderOracleJavaFolder="${AppPackageFolder}"/Contents/Java
  10. # set Oracle's Resources folderOracleResourcesFolder="${AppPackageFolder}"/Contents/Resources
  11. # set path to Info.plist in bundleInfoPlistFile="${AppPackageFolder}"/Contents/Info.plist
  12. # set the default JVM Version to a null stringJVMVersion=""JVMMaxVersion=""# function 'plist_get()'## read a specific Plist key with 'PlistBuddy' utility## @param1 the Plist key with leading colon ':'# @return the value as String or Array################################################################################plist_get(){
  13. /usr/libexec/PlistBuddy -c"print $1""${InfoPlistFile}"2> /dev/null
  14. }# function 'plist_get_java()'## read a specific Plist key with 'PlistBuddy' utility# in the 'Java' or 'JavaX' dictionary (<dict>)## @param1 the Plist :Java(X):Key with leading colon ':'# @return the value as String or Array################################################################################plist_get_java(){
  15. plist_get ${JavaKey:-":Java"}$1}# read Info.plist and extract JVM options############################################# read the program name from CFBundleNameCFBundleName=$(plist_get ':CFBundleName')# read the icon file nameCFBundleIconFile=$(plist_get ':CFBundleIconFile')# check Info.plist for Apple style Java keys -> if key :Java is present, parse in apple mode
  16. /usr/libexec/PlistBuddy -c"print :Java""${InfoPlistFile}"> /dev/null 2>&1exitcode=$?JavaKey=":Java"# if no :Java key is present, check Info.plist for universalJavaApplication style JavaX keys -> if key :JavaX is present, parse in apple modeif[$exitcode-ne0];then
  17. /usr/libexec/PlistBuddy -c"print :JavaX""${InfoPlistFile}"> /dev/null 2>&1exitcode=$?JavaKey=":JavaX"fi# read 'Info.plist' file in Apple style if exit code returns 0 (true, ':Java' key is present)if[$exitcode-eq0];then
  18. stub_logger "[PlistStyle] Apple"# set Java and Resources folderJavaFolder="${AppleJavaFolder}"ResourcesFolder="${AppleResourcesFolder}"APP_PACKAGE="${AppPackageFolder}"JAVAROOT="${AppleJavaFolder}"USER_HOME="$HOME"# read the Java WorkingDirectoryJVMWorkDir=$(plist_get_java ':WorkingDirectory'|xargs)# set Working Directory based upon PList valueif[[!-z${JVMWorkDir}]];thenWorkingDirectory="${JVMWorkDir}"else# AppPackageRoot is the standard WorkingDirectory when the script is startedWorkingDirectory="${AppPackageRoot}"fi# expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOMEWorkingDirectory=$(evalecho"${WorkingDirectory}")# read the MainClass nameJVMMainClass="$(plist_get_java ':MainClass')"# read the SplashFile nameJVMSplashFile=$(plist_get_java ':SplashFile')# read the JVM Properties as an array and retain spacesIFS=$'\t\n'JVMOptions=($(xargs-n1<<<$(plist_get_java ':Properties'|grep" ="|sed's/^ */-D/g'|sed-E's/ = (.*)$/="\1"/g')))unsetIFS# post processing of the array follows further below...# read the ClassPath in either Array or String styleJVMClassPath_RAW=$(plist_get_java ':ClassPath'|xargs)if[[$JVMClassPath_RAW== *Array* ]];thenJVMClassPath=.$(plist_get_java ':ClassPath'|grep" "|sed's/^ */:/g'|tr-d'\n'|xargs)elseJVMClassPath=${JVMClassPath_RAW}fi# expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOMEJVMClassPath=$(evalecho"${JVMClassPath}")# read the JVM Options in either Array or String styleJVMDefaultOptions_RAW=$(plist_get_java ':VMOptions'|xargs)if[[$JVMDefaultOptions_RAW== *Array* ]];thenJVMDefaultOptions=$(plist_get_java ':VMOptions'|grep" "|sed's/^ */ /g'|tr-d'\n'|xargs)elseJVMDefaultOptions=${JVMDefaultOptions_RAW}fi# read StartOnMainThread and add as -XstartOnFirstThreadJVMStartOnMainThread=$(plist_get_java ':StartOnMainThread')if["${JVMStartOnMainThread}"=="true"];thenJVMDefaultOptions+=" -XstartOnFirstThread"fi# read the JVM Arguments in either Array or String style (#76) and retain spacesIFS=$'\t\n'MainArgs_RAW=$(plist_get_java ':Arguments'|xargs)if[[$MainArgs_RAW== *Array* ]];thenMainArgs=($(xargs-n1<<<$(plist_get_java ':Arguments'|tr-d'\n'|sed-E's/Array \{ *(.*) *\}/\1/g'|sed's/ */ /g')))elseMainArgs=($(xargs-n1<<<$(plist_get_java ':Arguments')))fiunsetIFS# post processing of the array follows further below...# read the Java version we want to findJVMVersion=$(plist_get_java ':JVMVersion'|xargs)# post processing of the version string follows below...# read 'Info.plist' file in Oracle style
  19. else
  20. stub_logger "[PlistStyle] Oracle"
  21. # set Working Directory and Java and Resources folder
  22. JavaFolder="${OracleJavaFolder}"
  23. ResourcesFolder="${OracleResourcesFolder}"
  24. WorkingDirectory="${OracleJavaFolder}"
  25. APP_ROOT="${AppPackageFolder}"
  26. # read the MainClass name
  27. JVMMainClass="$(plist_get ':JVMMainClassName')"
  28. # read the SplashFile name
  29. JVMSplashFile=$(plist_get ':JVMSplashFile')
  30. # read the JVM Options as an array and retain spaces
  31. IFS=$'\t\n'
  32. JVMOptions=($(plist_get ':JVMOptions' | grep " " | sed 's/^ *//g'))
  33. unset IFS
  34. # post processing of the array follows further below...
  35. # read the ClassPath in either Array or String style
  36. JVMClassPath_RAW=$(plist_get ':JVMClassPath')
  37. if [[ $JVMClassPath_RAW == *Array* ]] ; then
  38. JVMClassPath=.$(plist_get ':JVMClassPath' | grep " " | sed 's/^ */:/g' | tr -d '\n' | xargs)
  39. # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
  40. JVMClassPath=$(eval echo "${JVMClassPath}")
  41. elif [[ ! -z ${JVMClassPath_RAW} ]] ; then
  42. JVMClassPath=${JVMClassPath_RAW}
  43. # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
  44. JVMClassPath=$(eval echo "${JVMClassPath}")
  45. else
  46. #default: fallback to OracleJavaFolder
  47. JVMClassPath="${JavaFolder}/*"
  48. # Do NOT expand the default 'AppName.app/Contents/Java/*' classpath (#42)
  49. fi
  50. # read the JVM Default Options
  51. JVMDefaultOptions=$(plist_get ':JVMDefaultOptions' | grep -o " \-.*" | tr -d '\n' | xargs)
  52. # read the Main Arguments from JVMArguments key as an array and retain spaces (see #46 for naming details)
  53. IFS=$'\t\n'
  54. MainArgs=($(xargs -n1 <<<$(plist_get ':JVMArguments' | tr -d '\n' | sed -E 's/Array \{ *(.*) *\}/\1/g' | sed 's/ */ /g')))
  55. unset IFS
  56. # post processing of the array follows further below...
  57. # read the Java version we want to find
  58. JVMVersion=$(plist_get ':JVMVersion' | xargs)
  59. # post processing of the version string follows below...
  60. fi
  61. # (#75) check for undefined icons or icon names without .icns extension and prepare
  62. # an osascript statement for those cases when the icon can be shown in the dialog
  63. DialogWithIcon=""
  64. if [ ! -z ${CFBundleIconFile} ]; then
  65. if [[ ${CFBundleIconFile} == *.icns ]] && [[ -f "${ResourcesFolder}/${CFBundleIconFile}" ]] ; then
  66. DialogWithIcon=" with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
  67. elif [[ ${CFBundleIconFile} != *.icns ]] && [[ -f "${ResourcesFolder}/${CFBundleIconFile}.icns" ]] ; then
  68. CFBundleIconFile+=".icns"
  69. DialogWithIcon=" with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
  70. fi
  71. fi
  72. # JVMVersion: post processing and optional splitting
  73. if [[ ${JVMVersion} == *";"* ]]; then
  74. minMaxArray=(${JVMVersion//;/ })
  75. JVMVersion=${minMaxArray[0]//+}
  76. JVMMaxVersion=${minMaxArray[1]//+}
  77. fi
  78. stub_logger "[JavaRequirement] JVM minimum version: ${JVMVersion}"
  79. stub_logger "[JavaRequirement] JVM maximum version: ${JVMMaxVersion}"
  80. # MainArgs: replace occurences of $APP_ROOT with its content
  81. MainArgsArr=()
  82. for i in "${MainArgs[@]}"
  83. do
  84. MainArgsArr+=("$(eval echo "$i")")
  85. done
  86. # JVMOptions: replace occurences of $APP_ROOT with its content
  87. JVMOptionsArr=()
  88. for i in "${JVMOptions[@]}"
  89. do
  90. JVMOptionsArr+=("$(eval echo "$i")")
  91. done
  92. # internationalized messages
  93. ############################################
  94. LANG=$(defaults read -g AppleLocale)
  95. stub_logger "[Language] $LANG"
  96. # French localization
  97. if [[ $LANG == fr* ]] ; then
  98. MSG_ERROR_LAUNCHING="ERREUR au lancement de '${CFBundleName}'."
  99. MSG_MISSING_MAINCLASS="'MainClass' n'est pas spécifié.\nL'application Java ne peut pas être lancée."
  100. MSG_JVMVERSION_REQ_INVALID="La syntaxe de la version de Java demandée est invalide: %s\nVeuillez contacter le développeur de l'application."
  101. MSG_NO_SUITABLE_JAVA="La version de Java installée sur votre système ne convient pas.\nCe programme nécessite Java %s"
  102. MSG_JAVA_VERSION_OR_LATER="ou ultérieur"
  103. MSG_JAVA_VERSION_LATEST="(dernière mise à jour)"
  104. MSG_JAVA_VERSION_MAX="à %s"
  105. MSG_NO_SUITABLE_JAVA_CHECK="Merci de bien vouloir installer la version de Java requise."
  106. MSG_INSTALL_JAVA="Java doit être installé sur votre système.\nRendez-vous sur java.com et suivez les instructions d'installation..."
  107. MSG_LATER="Plus tard"
  108. MSG_VISIT_JAVA_DOT_COM="Java by Oracle"
  109. MSG_VISIT_ADOPTOPENJDK="Java by AdoptOpenJDK"
  110. # German localization
  111. elif [[ $LANG == de* ]] ; then
  112. MSG_ERROR_LAUNCHING="FEHLER beim Starten von '${CFBundleName}'."
  113. MSG_MISSING_MAINCLASS="Die 'MainClass' ist nicht spezifiziert!\nDie Java-Anwendung kann nicht gestartet werden!"
  114. MSG_JVMVERSION_REQ_INVALID="Die Syntax der angeforderten Java-Version ist ungültig: %s\nBitte kontaktieren Sie den Entwickler der App."
  115. MSG_NO_SUITABLE_JAVA="Es wurde keine passende Java-Version auf Ihrem System gefunden!\nDieses Programm benötigt Java %s"
  116. MSG_JAVA_VERSION_OR_LATER="oder neuer"
  117. MSG_JAVA_VERSION_LATEST="(neuste Unterversion)"
  118. MSG_JAVA_VERSION_MAX="bis %s"
  119. MSG_NO_SUITABLE_JAVA_CHECK="Stellen Sie sicher, dass die angeforderte Java-Version installiert ist."
  120. MSG_INSTALL_JAVA="Auf Ihrem System muss die 'Java'-Software installiert sein.\nBesuchen Sie java.com für weitere Installationshinweise."
  121. MSG_LATER="Später"
  122. MSG_VISIT_JAVA_DOT_COM="Java von Oracle"
  123. MSG_VISIT_ADOPTOPENJDK="Java von AdoptOpenJDK"
  124. # Simplifyed Chinese localization
  125. elif [[ $LANG == zh* ]] ; then
  126. MSG_ERROR_LAUNCHING="无法启动 '${CFBundleName}'."
  127. MSG_MISSING_MAINCLASS="没有指定 'MainClass'!\nJava程序无法启动!"
  128. MSG_JVMVERSION_REQ_INVALID="Java版本参数语法错误: %s\n请联系该应用的开发者。"
  129. MSG_NO_SUITABLE_JAVA="没有在系统中找到合适的Java版本!\n必须安装Java %s才能够使用该程序!"
  130. MSG_JAVA_VERSION_OR_LATER="及以上版本"
  131. MSG_JAVA_VERSION_LATEST="(最新版本)"
  132. MSG_JAVA_VERSION_MAX="最高为 %s"
  133. MSG_NO_SUITABLE_JAVA_CHECK="请确保系统中安装了所需的Java版本"
  134. MSG_INSTALL_JAVA="你需要在Mac中安装Java运行环境!\n访问 java.com 了解如何安装。"
  135. MSG_LATER="稍后"
  136. MSG_VISIT_JAVA_DOT_COM="Java by Oracle"
  137. MSG_VISIT_ADOPTOPENJDK="Java by AdoptOpenJDK"
  138. # English default localization
  139. else
  140. MSG_ERROR_LAUNCHING="ERROR launching '${CFBundleName}'."
  141. MSG_MISSING_MAINCLASS="'MainClass' isn't specified!\nJava application cannot be started!"
  142. MSG_JVMVERSION_REQ_INVALID="The syntax of the required Java version is invalid: %s\nPlease contact the App developer."
  143. MSG_NO_SUITABLE_JAVA="No suitable Java version found on your system!\nThis program requires Java %s"
  144. MSG_JAVA_VERSION_OR_LATER="or later"
  145. MSG_JAVA_VERSION_LATEST="(latest update)"
  146. MSG_JAVA_VERSION_MAX="up to %s"
  147. MSG_NO_SUITABLE_JAVA_CHECK="Make sure you install the required Java version."
  148. MSG_INSTALL_JAVA="You need to have JAVA installed on your Mac!\nVisit java.com for installation instructions..."
  149. MSG_LATER="Later"
  150. MSG_VISIT_JAVA_DOT_COM="Java by Oracle"
  151. MSG_VISIT_ADOPTOPENJDK="Java by AdoptOpenJDK"
  152. fi
  153. # function 'get_java_version_from_cmd()'
  154. #
  155. # returns Java version string from 'java -version' command
  156. # works for both old (1.8) and new (9) version schema
  157. #
  158. # @param1 path to a java JVM executable
  159. # @return the Java version number as displayed in 'java -version' command
  160. ################################################################################
  161. function get_java_version_from_cmd() {
  162. # second sed command strips " and -ea from the version string
  163. echo$("$1" -version 2>&1 | awk '/version/{print $3}' | sed -E 's/"//g;s/-ea//g')}# function 'extract_java_major_version()'## extract Java major version from a version string## @param1 a Java version number ('1.8.0_45') or requirement string ('1.8+')# @return the major version (e.g. '7', '8' or '9', etc.)################################################################################functionextract_java_major_version(){echo$(echo"$1"|sed-E 's/^1\.//;s/^([0-9]+)(-ea|(\.[0-9_.]{1,7})?)(-b[0-9]+-[0-9]+)?[+*]?$/\1/')
  164. }
  165. # function 'get_comparable_java_version()'
  166. #
  167. # return comparable version for a Java version number or requirement string
  168. #
  169. # @param1 a Java version number ('1.8.0_45') or requirement string ('1.8+')
  170. # @return an 8 digit numeral ('1.8.0_45'->'08000045'; '9.1.13'->'09001013')
  171. ################################################################################
  172. function get_comparable_java_version() {
  173. # cleaning: 1) remove leading '1.'; 2) remove build string (e.g. '-b14-468'); 3) remove 'a-Z' and '-*+' (e.g. '-ea'); 4) replace '_' with '.'
  174. local cleaned=$(echo "$1" | sed -E 's/^1\.//g;s/-b[0-9]+-[0-9]+$//g;s/[a-zA-Z+*\-]//g;s/_/./g')
  175. # splitting at '.' into an array
  176. local arr=( ${cleaned//./ } )
  177. # echo a string with left padded version numbers
  178. echo "$(printf '%02s' ${arr[0]})$(printf '%03s' ${arr[1]})$(printf '%03s' ${arr[2]})"
  179. }
  180. # function 'is_valid_requirement_pattern()'
  181. #
  182. # check whether the Java requirement is a valid requirement pattern
  183. #
  184. # supported requirements are for example:
  185. # - 1.6 requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
  186. # - 1.6* requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
  187. # - 1.6+ requires Java 6 or higher [1.6, 1.6.0_45, 1.8, 9, etc.]
  188. # - 1.6.0 requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
  189. # - 1.6.0_45 requires Java 6u45 [1.6.0_45]
  190. # - 1.6.0_45+ requires Java 6u45 or higher [1.6.0_45, 1.6.0_88, 1.8, etc.]
  191. # - 9 requires Java 9 (any update) [9.0.*, 9.1, 9.3, etc.]
  192. # - 9* requires Java 9 (any update) [9.0.*, 9.1, 9.3, etc.]
  193. # - 9+ requires Java 9 or higher [9.0, 9.1, 10, etc.]
  194. # - 9.1 requires Java 9.1 (any update) [9.1.*, 9.1.2, 9.1.13, etc.]
  195. # - 9.1* requires Java 9.1 (any update) [9.1.*, 9.1.2, 9.1.13, etc.]
  196. # - 9.1+ requires Java 9.1 or higher [9.1, 9.2, 10, etc.]
  197. # - 9.1.3 requires Java 9.1.3 [9.1.3]
  198. # - 9.1.3* requires Java 9.1.3 (any update) [9.1.3]
  199. # - 9.1.3+ requires Java 9.1.3 or higher [9.1.3, 9.1.4, 9.2.*, 10, etc.]
  200. # - 10-ea requires Java 10 (early access release)
  201. #
  202. # unsupported requirement patterns are for example:
  203. # - 1.2, 1.3, 1.9 Java 2, 3 are not supported
  204. # - 1.9 Java 9 introduced a new versioning scheme
  205. # - 6u45 known versioning syntax, but unsupported
  206. # - 9-ea*, 9-ea+ early access releases paired with */+
  207. # - 9., 9.*, 9.+ version ending with a .
  208. # - 9.1., 9.1.*, 9.1.+ version ending with a .
  209. # - 9.3.5.6 4 part version number is unsupported
  210. #
  211. # @param1 a Java requirement string ('1.8+')
  212. # @return boolean exit code: 0 (is valid), 1 (is not valid)
  213. ################################################################################
  214. function is_valid_requirement_pattern() {
  215. local java_req=$1
  216. java8pattern='1\.[4-8](\.[0-9]+)?(\.0_[0-9]+)?[*+]?'
  217. java9pattern='(9|1[0-9])(-ea|[*+]|(\.[0-9]+){1,2}[*+]?)?'
  218. # test matches either old Java versioning scheme (up to 1.8) or new scheme (starting with 9)
  219. if [[ ${java_req} =~ ^(${java8pattern}|${java9pattern})$ ]]; then
  220. return 0
  221. else
  222. return 1
  223. fi
  224. }
  225. # determine which JVM to use
  226. ############################################
  227. # default Apple JRE plugin path (< 1.6)
  228. apple_jre_plugin="/Library/Java/Home/bin/java"
  229. apple_jre_version=$(get_java_version_from_cmd "${apple_jre_plugin}")
  230. # default Oracle JRE plugin path (>= 1.7)
  231. oracle_jre_plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"
  232. oracle_jre_version=$(get_java_version_from_cmd "${oracle_jre_plugin}")
  233. # first check system variable "$JAVA_HOME" -> has precedence over any other System JVM
  234. stub_logger '[JavaSearch] Checking for$JAVA_HOME...'
  235. if [ -n "$JAVA_HOME" ] ; then
  236. stub_logger "[JavaSearch] ... found JAVA_HOME with value $JAVA_HOME"
  237. # PR 26: Allow specifying "$JAVA_HOME" relative to "$AppPackageFolder"
  238. # which allows for bundling a custom version of Java inside your app!
  239. if [[ $JAVA_HOME == /* ]] ; then
  240. # if "$JAVA_HOME" starts with a Slash it's an absolute path
  241. JAVACMD="$JAVA_HOME/bin/java"else# otherwise it's a relative path to "$AppPackageFolder"
  242. JAVACMD="$AppPackageFolder/$JAVA_HOME/bin/java"
  243. fi
  244. JAVACMD_version=$(get_comparable_java_version $(get_java_version_from_cmd "${JAVACMD}"))
  245. else
  246. stub_logger "[JavaSearch]... didn't found JAVA_HOME"
  247. fi
  248. # check for any other or a specific Java version
  249. # also if $JAVA_HOME exists but isn't executable
  250. if[-z"${JAVACMD}"]||[!-x"${JAVACMD}"];then
  251. stub_logger "[JavaSearch] Checking for JavaVirtualMachines on the system ..."# reset variablesJAVACMD=""JAVACMD_version=""# first check whether JVMVersion string is a valid requirement stringif[!-z"${JVMVersion}"]&&! is_valid_requirement_pattern ${JVMVersion};thenMSG_JVMVERSION_REQ_INVALID_EXPANDED=$(printf"${MSG_JVMVERSION_REQ_INVALID}""${JVMVersion}")# log exit cause
  252. stub_logger "[EXIT 4] ${MSG_JVMVERSION_REQ_INVALID_EXPANDED}"# display error message with AppleScript
  253. osascript -e"tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_JVMVERSION_REQ_INVALID_EXPANDED}\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1${DialogWithIcon}"# exit with errorexit4fi# then check whether JVMMaxVersion string is a valid requirement stringif[!-z"${JVMMaxVersion}"]&&! is_valid_requirement_pattern ${JVMMaxVersion};thenMSG_JVMVERSION_REQ_INVALID_EXPANDED=$(printf"${MSG_JVMVERSION_REQ_INVALID}""${JVMMaxVersion}")# log exit cause
  254. stub_logger "[EXIT 5] ${MSG_JVMVERSION_REQ_INVALID_EXPANDED}"# display error message with AppleScript
  255. osascript -e"tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_JVMVERSION_REQ_INVALID_EXPANDED}\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1${DialogWithIcon}"# exit with errorexit5fi# find installed JavaVirtualMachines (JDK + JRE)allJVMs=()# read JDK's from '/usr/libexec/java_home -V' commandwhileread-r line;doversion=$(echo $line |awk-F$',''{print $1;}')path=$(echo $line |awk-F$'" ''{print $2;}')path+="/bin/java"allJVMs+=("$version:$path")done<<(/usr/libexec/java_home -V2>&1|grep'^[[:space:]]')# unset while loop variablesunset version path
  256. # add Apple JRE if availableif[-x"${apple_jre_plugin}"];thenallJVMs+=("$apple_jre_version:$apple_jre_plugin")fi# add Oracle JRE if availableif[-x"${oracle_jre_plugin}"];thenallJVMs+=("$oracle_jre_version:$oracle_jre_plugin")fi# debug outputforiin"${allJVMs[@]}"do
  257. stub_logger "[JavaSearch] ... found JVM: $i"done# determine JVMs matching the min/max version requirementminC=$(get_comparable_java_version ${JVMVersion})maxC=$(get_comparable_java_version ${JVMMaxVersion})matchingJVMs=()foriin"${allJVMs[@]}"do# split JVM string at ':' delimiter to retain spaces in $path substringIFS=: arr=($i);unsetIFS# [0] JVM version numberver=${arr[0]}# comparable JVM version numbercomp=$(get_comparable_java_version $ver)# [1] JVM pathpath="${arr[1]}"# construct string item for adding to the "matchingJVMs" array
  258. item="$comp:$ver:$path"
  259. # pre-requisite: current version number needs to be greater than min version number
  260. if [ "$comp" -ge "$minC" ] ; then
  261. # perform max version checks if max version requirement is present
  262. if [ ! -z ${JVMMaxVersion} ] ; then
  263. # max version requirement ends with '*' modifier
  264. if [[ ${JVMMaxVersion} == *\* ]] ; then
  265. # use the '*' modifier from the max version string as wildcard for a 'starts with' comparison
  266. # and check whether the current version number starts with the max version wildcard string
  267. if [[ ${ver} == ${JVMMaxVersion} ]]; then
  268. matchingJVMs+=("$item")
  269. # or whether the current comparable version is lower than the comparable max version
  270. elif [ "$comp" -le "$maxC" ] ; then
  271. matchingJVMs+=("$item")
  272. fi
  273. # max version requirement ends with '+' modifier -> always add this version if it's greater than $min
  274. # because a max requirement with + modifier doesn't make sense
  275. elif [[ ${JVMMaxVersion} == *+ ]] ; then
  276. matchingJVMs+=("$item")
  277. # matches 6 zeros at the end of the max version string (e.g. for 1.8, 9)
  278. # -> then the max version string should be treated like with a '*' modifier at the end
  279. #elif [[ ${maxC} =~ ^[0-9]{2}0{6}$ ]] && [ "$comp" -le $(( ${maxC#0} +999)) ] ; then
  280. # matchingJVMs+=("$item")
  281. # matches 3 zeros at the end of the max version string (e.g. for 9.1, 10.3)
  282. # -> then the max version string should be treated like with a '*' modifier at the end
  283. #elif [[ ${maxC} =~ ^[0-9]{5}0{3}$ ]] && [ "$comp" -le "${maxC}" ] ; then
  284. # matchingJVMs+=("$item")
  285. # matches standard requirements without modifier
  286. elif [ "$comp" -le "$maxC" ]; then
  287. matchingJVMs+=("$item")
  288. fi
  289. # no max version requirement:
  290. # min version requirement ends with '+' modifier
  291. # -> always add the current version because it's greater than $min
  292. elif [[ ${JVMVersion} == *+ ]] ; then
  293. matchingJVMs+=("$item")
  294. # min version requirement ends with '*' modifier
  295. # -> use the '*' modifier from the min version string as wildcard for a 'starts with' comparison
  296. # and check whether the current version number starts with the min version wildcard string
  297. elif [[ ${JVMVersion} == *\* ]] ; then
  298. if [[ ${ver} == ${JVMVersion} ]] ; then
  299. matchingJVMs+=("$item")
  300. fi
  301. # compare the min version against the current version with an additional * wildcard for a 'starts with' comparison
  302. # -> e.g. add 1.8.0_44 when the requirement is 1.8
  303. elif [[ ${ver} == ${JVMVersion}* ]] ; then
  304. matchingJVMs+=("$item")
  305. fi
  306. fi
  307. done
  308. # unset for loop variables
  309. unset arr ver comp path item
  310. # debug output
  311. for i in "${matchingJVMs[@]}"
  312. do
  313. stub_logger "[JavaSearch]... ... matches all requirements: $i"
  314. done
  315. # sort the matching JavaVirtualMachines by version number
  316. # https://stackoverflow.com/a/11789688/1128689
  317. IFS=$'\n' matchingJVMs=($(sort-nr<<<"${matchingJVMs[*]}"))
  318. unset IFS
  319. # get the highest matching JVM
  320. for ((i =0; i < ${#matchingJVMs[@]}; i++));
  321. do
  322. # split JVM string at ':' delimiter to retain spaces in $path substring
  323. IFS=: arr=(${matchingJVMs[$i]}) ; unset IFS
  324. # [0] comparable JVM version number
  325. comp=${arr[0]}
  326. # [1] JVM version number
  327. ver=${arr[1]}
  328. # [2] JVM path
  329. path="${arr[2]}"
  330. # use current value as JAVACMD if it's executable
  331. if [ -x "$path" ] ; then
  332. JAVACMD="$path"
  333. JAVACMD_version=$comp
  334. break
  335. fi
  336. done
  337. # unset for loop variables
  338. unset arr comp ver path
  339. fi
  340. # log the Java Command and the extracted version number
  341. stub_logger "[JavaCommand]'$JAVACMD'"
  342. stub_logger "[JavaVersion]$(get_java_version_from_cmd "${JAVACMD}")${JAVACMD_version:+/ $JAVACMD_version}"
  343. if [ -z "${JAVACMD}" ] || [ ! -x "${JAVACMD}" ] ; then
  344. # different error messages when a specific JVM was required
  345. if [ ! -z "${JVMVersion}" ] ; then
  346. # display human readable java version (#28)
  347. java_version_hr=$(echo ${JVMVersion} | sed -E 's/^1\.([0-9+*]+)$/ \1/g' | sed "s/+/ ${MSG_JAVA_VERSION_OR_LATER}/;s/*/ ${MSG_JAVA_VERSION_LATEST}/")
  348. MSG_NO_SUITABLE_JAVA_EXPANDED=$(printf"${MSG_NO_SUITABLE_JAVA}""${java_version_hr}").
  349. if [ ! -z "${JVMMaxVersion}" ] ; then
  350. java_version_hr=$(extract_java_major_version ${JVMVersion})
  351. java_version_max_hr=$(echo ${JVMMaxVersion} | sed -E 's/^1\.([0-9+*]+)$/ \1/g' | sed "s/+//;s/*/ ${MSG_JAVA_VERSION_LATEST}/")
  352. MSG_NO_SUITABLE_JAVA_EXPANDED="$(printf"${MSG_NO_SUITABLE_JAVA}""${java_version_hr}")$(printf"${MSG_JAVA_VERSION_MAX}""${java_version_max_hr}")"
  353. fi
  354. # log exit cause
  355. stub_logger "[EXIT 3]${MSG_NO_SUITABLE_JAVA_EXPANDED}"
  356. # display error message with AppleScript
  357. osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_NO_SUITABLE_JAVA_EXPANDED}\n${MSG_NO_SUITABLE_JAVA_CHECK}\" with title \"${CFBundleName}\" buttons {\" OK \", \"${MSG_VISIT_JAVA_DOT_COM}\", \"${MSG_VISIT_ADOPTOPENJDK}\"} default button 1${DialogWithIcon}" \
  358. -e "set response to button returned of the result" \
  359. -e "if response is \"${MSG_VISIT_JAVA_DOT_COM}\" thenopen location \"https://www.java.com/download/\"" \
  360. -e "if response is \"${MSG_VISIT_ADOPTOPENJDK}\" thenopen location \"https://adoptopenjdk.net/releases.html\""
  361. # exit with error
  362. exit 3
  363. else
  364. # log exit cause
  365. stub_logger "[EXIT 1]${MSG_ERROR_LAUNCHING}"
  366. # display error message with AppleScript
  367. osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_INSTALL_JAVA}\" with title \"${CFBundleName}\" buttons {\"${MSG_LATER}\", \"${MSG_VISIT_JAVA_DOT_COM}\", \"${MSG_VISIT_ADOPTOPENJDK}\"} default button 1${DialogWithIcon}" \
  368. -e "set response to button returned of the result" \
  369. -e "if response is \"${MSG_VISIT_JAVA_DOT_COM}\" thenopen location \"https://www.java.com/download/\"" \
  370. -e "if response is \"${MSG_VISIT_ADOPTOPENJDK}\" thenopen location \"https://adoptopenjdk.net/releases.html\""
  371. # exit with error
  372. exit 1
  373. fi
  374. fi
  375. # MainClass check
  376. ############################################
  377. if [ -z "${JVMMainClass}" ]; then
  378. # log exit cause
  379. stub_logger "[EXIT 2]${MSG_MISSING_MAINCLASS}"
  380. # display error message with AppleScript
  381. osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_MISSING_MAINCLASS}\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1${DialogWithIcon}"
  382. # exit with error
  383. exit 2
  384. fi
  385. # execute $JAVACMD and do some preparation
  386. ############################################
  387. # enable drag&drop to the dock icon
  388. export CFProcessPath="$0"
  389. # remove Apples ProcessSerialNumber from passthru arguments (#39)
  390. if [[ "$*" == -psn* ]] ; then
  391. ArgsPassthru=()
  392. else
  393. ArgsPassthru=("$@")
  394. fi
  395. # change to Working Directory based upon Apple/Oracle Plist info
  396. cd "${WorkingDirectory}" || exit 13
  397. stub_logger "[WorkingDirectory]${WorkingDirectory}"
  398. # execute Java and set
  399. # - classpath
  400. # - splash image
  401. # - dock icon
  402. # - app name
  403. # - JVM options / properties (-D)
  404. # - JVM default options (-X)
  405. # - main class
  406. # - main class arguments
  407. # - passthrough arguments from Terminal or Drag'n'Drop to Finder icon
  408. stub_logger "[Exec]\"$JAVACMD\" -cp\"${JVMClassPath}\" -splash:\"${ResourcesFolder}/${JVMSplashFile}\" -Xdock:icon=\"${ResourcesFolder}/${CFBundleIconFile}\" -Xdock:name=\"${CFBundleName}\" ${JVMOptionsArr:+$(printf "'%s' " "${JVMOptionsArr[@]}")}${JVMDefaultOptions:+$JVMDefaultOptions }${JVMMainClass}${MainArgsArr:+ $(printf "'%s' " "${MainArgsArr[@]}")}${ArgsPassthru:+ $(printf "'%s' " "${ArgsPassthru[@]}")}"
  409. exec "${JAVACMD}" \
  410. -cp "${JVMClassPath}" \
  411. -splash:"${ResourcesFolder}/${JVMSplashFile}" \
  412. -Xdock:icon="${ResourcesFolder}/${CFBundleIconFile}" \
  413. -Xdock:name="${CFBundleName}" \
  414. ${JVMOptionsArr:+"${JVMOptionsArr[@]}" }\
  415. ${JVMDefaultOptions:+$JVMDefaultOptions }\
  416. "${JVMMainClass}"\
  417. ${MainArgsArr:+ "${MainArgsArr[@]}"}\
  418. ${ArgsPassthru:+ "${ArgsPassthru[@]}"}

2.3 关于使用

打开软件,选择自己要反编译的文件即可;
在这里插入图片描述
确认文件打开之后,我们就能看到反编译的源代码了;
在这里插入图片描述

三、最后

Windows的宝子们,方法上大同小异的,除了下载包区分一下,其他基本一致的问题;

标签: macos java 策略模式

本文转载自: https://blog.csdn.net/qq_39611212/article/details/140000642
版权归原作者 秋刀prince 所有, 如有侵权,请联系我们删除。

“【JD-GUI】MacOS 中使用Java反编译工具JD-GUI”的评论:

还没有评论