Mac Big Sur 升级后 JD-GUI 无法打开的问题修复

升级 Big Sur 后发现JD-GUI 打开报错:

image-20201226100014573

ERROR launching ‘JD-GUI’

No suitable Java version found on your system!
This program requires Java 1.8+
Make sure you install the required Java version.

参见 JD-GUI 的issue :Update universalJavaApplicationStub to be able to launch on macOS Big Sur #336
我们需要替换一个文件;universalJavaApplicationStub.sh (version 3.0.6),问题出现的原因是在 MacOS Big Sur的版本中,/usr/libexec/java_home组合选项-F-v不再支持有+的字符串查看 JVM 版本,软件的的 info.plist 如果java的版本是一个包含+的字符串,如1.8+,则会出现错误无法开启软件。解释的可能不是很清楚,用命令演示可能会更直接。

/usr/libexec/java_home很容易找到你在你的电脑上安装的jdk版本,我们可以通过/usr/libexec/java_home -F 查看:

Matching Java Virtual Machines (2):

1.8.0_221 (x86_64) “Oracle Corporation” - “Java SE 8” /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home

1.8.0_91 (x86_64) “Oracle Corporation” - “Java SE 8” /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home

上面命令中-F就是显示已安装的全部的 JVM 版本,JVM括号中的2表示我的电脑安装两个JVM,最后一行显示的当前运行的版本。-v参数就是查询指定版本的JVM。在MacOS Big Sur 以前,/usr/libexec/java_home通过-v参数支持“模糊”匹配,比如/usr/libexec/java_home -F -v'1.8+'查找1.8以及1.8以上的JVM,也支持*/usr/libexec/java_home -F -v'1.8*'。MacOS Big Sur中,该命令不支持包含有+*的组合选项了,只支持精确的查找:/usr/libexec/java_home -F -v'1.8'或者“模糊”查找/usr/libexec/java_home -v'1.8+'

吧啦吧啦说了一堆,也就是查找的JVM版本字符串要用+就不能用-F,否则就不要用+

解释清楚了问题的原因,我们就可以根据universalJavaApplicationStub.sh作者提供的脚本进行修复了。脚本内容如下,对于有些打不开github的同学可以直接复制下面的脚本,如果能打开github,点击传送门下载:

替换 pierswalter 的脚本之后出现下面的错误:

ERROR launching ‘JD-GUI’!

You need to have JAVA installed on your Mac! Visit http://java.com for more information…

![image-20201226104949098](file:///Users/zhoujunwen/Documents/%E6%8A%80%E6%9C%AF%E6%80%BB%E7%BB%93/image/jd-gui-notfound-java.png?lastModify=1609325062)

替换tofi86的脚本也会出现如下信息:

无法启动 ‘JD-GUI’.

没有在系统中找到合适的Java版本! 必须安装Java 8 及以上版本才能够使用该程序!. 请确保系统中安装了所需的Java版本

![image-20201226105113830](file:///Users/zhoujunwen/Documents/%E6%8A%80%E6%9C%AF%E6%80%BB%E7%BB%93/image/jd-gui-not-found-java.png?lastModify=1609325062)

image-20201226104617127

上图为pierswalte提供的修改脚本和原来的脚本的新(右)旧(左)脚本的差异对比结果。在新(右边)旧(左边)版本的比较图中,作者修改了判断命令,却没有修改最终执行的命令。所以在新版中要把-F参数删掉。下面脚本我已做了修改。最后,在pierswalte的基础上我提交了一个修改过的版本,不知道会不会被jd-gui官方仓库采纳。

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/sh
##################################################################################
# #
# universalJavaApplicationStub #
# #
# #
# A shellscript 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 2015-05-15 #
# @version 0.9.0 #
# #
# #
##################################################################################
# #
# #
# The MIT License (MIT) #
# #
# Copyright (c) 2015 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. #
# #
##################################################################################




#
# resolve symlinks
#####################

PRG=$0

while [ -h "$PRG" ]; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '^.*-> \(.*\)$' 2>/dev/null`
if expr "$link" : '^/' 2> /dev/null >/dev/null; then
PRG="$link"
else
PRG="`dirname "$PRG"`/$link"
fi
done

# set the directory abspath of the current shell script
PROGDIR=`dirname "$PRG"`




#
# set files and folders
############################################

# the absolute path of the app package
cd "$PROGDIR"/../../
AppPackageFolder=`pwd`

# the base path of the app package
cd ..
AppPackageRoot=`pwd`

# set Apple's Java folder
AppleJavaFolder="${AppPackageFolder}"/Contents/Resources/Java

# set Apple's Resources folder
AppleResourcesFolder="${AppPackageFolder}"/Contents/Resources

# set Oracle's Java folder
OracleJavaFolder="${AppPackageFolder}"/Contents/Java

# set Oracle's Resources folder
OracleResourcesFolder="${AppPackageFolder}"/Contents/Resources

# set path to Info.plist in bundle
InfoPlistFile="${AppPackageFolder}"/Contents/Info.plist

# set the default JVM Version to a null string
JVMVersion=""



#
# read Info.plist and extract JVM options
############################################


# read the program name from CFBundleName
CFBundleName=`/usr/libexec/PlistBuddy -c "print :CFBundleName" "${InfoPlistFile}"`

# read the icon file name
CFBundleIconFile=`/usr/libexec/PlistBuddy -c "print :CFBundleIconFile" "${InfoPlistFile}"`


# check Info.plist for Apple style Java keys -> if key :Java is present, parse in apple mode
/usr/libexec/PlistBuddy -c "print :Java" "${InfoPlistFile}" > /dev/null 2>&1
exitcode=$?
JavaKey=":Java"

# if no :Java key is present, check Info.plist for universalJavaApplication style JavaX keys -> if key :JavaX is present, parse in apple mode
if [ $exitcode -ne 0 ]; then
/usr/libexec/PlistBuddy -c "print :JavaX" "${InfoPlistFile}" > /dev/null 2>&1
exitcode=$?
JavaKey=":JavaX"
fi


# read Info.plist in Apple style if exit code returns 0 (true, :Java key is present)
if [ $exitcode -eq 0 ]; then

# set Java and Resources folder
JavaFolder="${AppleJavaFolder}"
ResourcesFolder="${AppleResourcesFolder}"

APP_PACKAGE="${AppPackageFolder}"
JAVAROOT="${AppleJavaFolder}"
USER_HOME="$HOME"


# read the Java WorkingDirectory
JVMWorkDir=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:WorkingDirectory" "${InfoPlistFile}" 2> /dev/null | xargs`

# set Working Directory based upon Plist info
if [[ ! -z ${JVMWorkDir} ]]; then
WorkingDirectory="${JVMWorkDir}"
else
# AppPackageRoot is the standard WorkingDirectory when the script is started
WorkingDirectory="${AppPackageRoot}"
fi

# expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
WorkingDirectory=`eval "echo ${WorkingDirectory}"`


# read the MainClass name
JVMMainClass=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:MainClass" "${InfoPlistFile}" 2> /dev/null`

# read the JVM Options
JVMOptions=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:Properties" "${InfoPlistFile}" 2> /dev/null | grep " =" | sed 's/^ */-D/g' | tr '\n' ' ' | sed 's/ */ /g' | sed 's/ = /=/g' | xargs`

# read StartOnMainThread
JVMStartOnMainThread=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:StartOnMainThread" "${InfoPlistFile}" 2> /dev/null`
if [ "${JVMStartOnMainThread}" == "true" ]; then
echo ${JVMStartOnMainThread} > ~/Desktop/test.txt
JVMOptions+=" -XstartOnFirstThread"
fi

# read the ClassPath in either Array or String style
JVMClassPath_RAW=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:ClassPath" "${InfoPlistFile}" 2> /dev/null`
if [[ $JVMClassPath_RAW == *Array* ]] ; then
JVMClassPath=.`/usr/libexec/PlistBuddy -c "print ${JavaKey}:ClassPath" "${InfoPlistFile}" 2> /dev/null | grep " " | sed 's/^ */:/g' | tr -d '\n' | xargs`
else
JVMClassPath=${JVMClassPath_RAW}
fi
# expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
JVMClassPath=`eval "echo ${JVMClassPath}"`

# read the JVM Default Options
JVMDefaultOptions=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:VMOptions" "${InfoPlistFile}" 2> /dev/null | xargs`

# read the JVM Arguments
JVMArguments=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:Arguments" "${InfoPlistFile}" 2> /dev/null | xargs`

# read the Java version we want to find
JVMVersion=`/usr/libexec/PlistBuddy -c "print ${JavaKey}:JVMVersion" "${InfoPlistFile}" 2> /dev/null | xargs`

# read Info.plist in Oracle style
else

# set Working Directory and Java and Resources folder
JavaFolder="${OracleJavaFolder}"
ResourcesFolder="${OracleResourcesFolder}"
WorkingDirectory="${OracleJavaFolder}"

APP_ROOT="${AppPackageFolder}"

# read the MainClass name
JVMMainClass=`/usr/libexec/PlistBuddy -c "print :JVMMainClassName" "${InfoPlistFile}" 2> /dev/null`

# read the JVM Options
JVMOptions=`/usr/libexec/PlistBuddy -c "print :JVMOptions" "${InfoPlistFile}" 2> /dev/null | grep " -" | tr -d '\n' | sed 's/ */ /g' | xargs`
# replace occurances of $APP_ROOT with it's content
JVMOptions=`eval "echo ${JVMOptions}"`

JVMClassPath="${JavaFolder}/*"

# read the JVM Default Options
JVMDefaultOptions=`/usr/libexec/PlistBuddy -c "print :JVMDefaultOptions" "${InfoPlistFile}" 2> /dev/null | grep -o "\-.*" | tr -d '\n' | xargs`

# read the JVM Arguments
JVMArguments=`/usr/libexec/PlistBuddy -c "print :JVMArguments" "${InfoPlistFile}" 2> /dev/null | tr -d '\n' | sed -E 's/Array \{ *(.*) *\}/\1/g' | sed 's/ */ /g' | xargs`
# replace occurances of $APP_ROOT with it's content
JVMArguments=`eval "echo ${JVMArguments}"`
fi




#
# find installed Java versions
#################################

# first check system variable "$JAVA_HOME"
if [ -n "$JAVA_HOME" ] ; then
JAVACMD="$JAVA_HOME/bin/java"

# check for specified JVMversion in "/usr/libexec/java_home" symlinks
elif [ ! -z ${JVMVersion} ] && [ -x /usr/libexec/java_home ] && /usr/libexec/java_home -F; then

if /usr/libexec/java_home -v ${JVMVersion} 2> /dev/null; then
JAVACMD="`/usr/libexec/java_home -v ${JVMVersion} 2> /dev/null`/bin/java"
else
# display error message with applescript
osascript -e "tell application \"System Events\" to display dialog \"ERROR launching '${CFBundleName}'\n\nNo suitable Java version found on your system!\nThis program requires Java ${JVMVersion}\nMake sure you install the required Java version.\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1 with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
# exit with error
exit 3
fi

# otherwise check "/usr/libexec/java_home" symlinks
elif [ -x /usr/libexec/java_home ] && /usr/libexec/java_home -F; then
JAVACMD="`/usr/libexec/java_home 2> /dev/null`/bin/java"

# otherwise check Java standard symlink (old Apple Java)
elif [ -h /Library/Java/Home ]; then
JAVACMD="/Library/Java/Home/bin/java"

# fallback: public JRE plugin (Oracle Java)
else
JAVACMD="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"
fi

# fallback fallback: /usr/bin/java
# but this would prompt to install deprecated Apple Java 6




#
# execute JAVA commandline and do some pre-checks
####################################################

# display error message if MainClassName is empty
if [ -z ${JVMMainClass} ]; then
# display error message with applescript
osascript -e "tell application \"System Events\" to display dialog \"ERROR launching '${CFBundleName}'!\n\n'MainClass' isn't specified!\nJava application cannot be started!\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1 with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
# exit with error
exit 2


# check whether $JAVACMD is a file and executable
elif [ -f "$JAVACMD" ] && [ -x "$JAVACMD" ] ; then

# enable drag&drop to the dock icon
export CFProcessPath="$0"

# change to Working Directory based upon Apple/Oracle Plist info
cd "${WorkingDirectory}"

# execute Java and set
# - classpath
# - dock icon
# - application name
# - JVM options
# - JVM default options
# - main class
# - JVM arguments
exec "$JAVACMD" \
-cp "${JVMClassPath}" \
-Xdock:icon="${ResourcesFolder}/${CFBundleIconFile}" \
-Xdock:name="${CFBundleName}" \
${JVMOptions:+$JVMOptions }\
${JVMDefaultOptions:+$JVMDefaultOptions }\
${JVMMainClass}\
${JVMArguments:+ $JVMArguments}


else

# display error message with applescript
osascript -e "tell application \"System Events\" to display dialog \"ERROR launching '${CFBundleName}'!\n\nYou need to have JAVA installed on your Mac!\nVisit http://java.com for more information...\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1 with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"

# and open java.com
open http://java.com

# exit with error
exit 1
fi

上面脚本复制日期:2020年12月26日10:30:00

脚本的commit信息:Latest commit 7e41892

如果不是这个版本,作者有可能做过修复,有条件的话请对比使用或者直接下载作者的脚本。

image-20201226103635029

image-20201226103524076

image-20201230192710874