osx mac jdk7 as highest priority
#!/bin/sh | |
set -e | |
# | |
# set oracle jdk7 as the highest-priority default java for all users and | |
# all future users on this machine. | |
# | |
# fuck you oracle for not releasing proper tools for deployment manageability | |
# | |
# License: This program is free software; you can redistribute it and/or | |
# modify it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 3 of the License, or (at your | |
# option) any later version. This program is distributed in the hope that it | |
# will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | |
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | |
# Public License for more details. | |
die() { | |
echo "$@" >&2 | |
exit 1 | |
} | |
uname | grep -q '^Darwin$' || die "$0 must run on OSX." | |
UUID=`/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/UUID/{print$3}'` | |
JDK6_FULL_VERSION=`/usr/libexec/java_home --version 1.6 --exec java -version 2>&1 | /usr/bin/awk '/Runtime/{print$6}' | /usr/bin/cut -d- -f 1-3` | |
if [ -n "$JDK6_FULL_VERSION" ]; then | |
JDK6_JVM_BUNDLE_ID=`/usr/libexec/java_home --version 1.6 -xml | /usr/bin/grep -o '[a-z0-9.]*java[a-z0-9.]*' | /usr/bin/uniq` | |
[ -z "$JDK6_JVM_BUNDLE_ID" ] && die "Could not locate the JDK6 bundleid" | |
JDK6_HOME="`/usr/libexec/java_home --version 1.6`" | |
[ -z "$JDK6_HOME" ] && die "Could not locate the JDK6 home" | |
fi | |
JDK7_FULL_VERSION=`/usr/libexec/java_home --version 1.7 --exec java -version 2>&1 | /usr/bin/grep java | /usr/bin/cut -d'"' -f2` | |
if [ -n "$JDK7_FULL_VERSION" ]; then | |
JDK7_JVM_BUNDLE_ID=`/usr/libexec/java_home --version 1.7 -xml | /usr/bin/grep -o '[a-z0-9.]*java[a-z0-9.]*' | /usr/bin/uniq` | |
[ -z "$JDK7_JVM_BUNDLE_ID" ] && die "Could not locate the JDK7 bundleid" | |
JDK7_HOME="`/usr/libexec/java_home --version 1.7`" | |
[ -z "$JDK7_HOME" ] && die "Could not locate the JDK7 home" | |
fi | |
setvm() { | |
base="$1" | |
plist="$base/Library/Preferences/ByHost/com.apple.java.JavaPreferences.$UUID.plist" | |
dir="`dirname \"$plist\"`" | |
/usr/bin/sudo mkdir -p "$dir" | |
user="`/usr/bin/sudo stat -s "$dir" | /usr/bin/sed 's/.*st_uid=\([0-9][0-9]*\) .*/\1/'`" | |
group="`/usr/bin/sudo stat -s "$dir" | /usr/bin/sed 's/.*st_gid=\([0-9][0-9]*\) .*/\1/'`" | |
if /usr/bin/sudo /usr/libexec/PlistBuddy -c 'Print :PreferredJVMOrder:Any:0:JVMVendor' "$plist" 2>/dev/null | /usr/bin/grep -q '^Oracle Corporation$' && \ | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c 'Print :PreferredJVMOrder:Any:0:JVMPlatformVersion' "$plist" 2>/dev/null | /usr/bin/grep -q '^1.7$' | |
then | |
echo " JVM is already set to JDK7" >&2 | |
return 0 | |
fi | |
if /usr/bin/sudo /usr/libexec/PlistBuddy -c 'Print :PreferredJVMOrder:Any' "$plist" >/dev/null 2>&1 | |
then | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Delete :PreferredJVMOrder:Any" "$plist" | |
fi | |
# changing JDK from default | |
JVM = 0 | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder Dict" "$plist" 2>/dev/null || true | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any array" "$plist" | |
if [ -d "$JDK7_HOME" ]; then | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM Dict" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMArch string x86_64" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMVersion string $JDK7_FULL_VERSION" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMPlatformVersion string 1.7" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMBundleID string $JDK7_JVM_BUNDLE_ID" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMVendor string 'Oracle Corporation'" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMHomePath string '$JDK7_HOME'" "$plist" | |
$JVM = $[$JVM + 1] | |
fi | |
if [ -d "$JDK6_HOME" ]; then | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM Dict" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMArch string x86_64" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMVersion string $JDK6_FULL_VERSION" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMPlatformVersion string 1.6" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMBundleID string $JDK6_JVM_BUNDLE_ID" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMVendor string 'Apple Inc.'" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMHomePath string '$JDK6_HOME'" "$plist" | |
$JVM = $[$JVM + 1] | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM Dict" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMArch string i386" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMVersion string $JDK6_FULL_VERSION" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMPlatformVersion string 1.6" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMBundleID string $JDK6_JVM_BUNDLE_ID" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMVendor string 'Apple Inc.'" "$plist" | |
/usr/bin/sudo /usr/libexec/PlistBuddy -c "Add :PreferredJVMOrder:Any:$JVM:JVMHomePath string '$JDK6_HOME'" "$plist" | |
$JVM = $[$JVM + 1] | |
fi | |
/usr/bin/sudo /usr/bin/plutil -convert binary1 "$plist" | |
/usr/bin/sudo /usr/sbin/chown -R $user:$group "$dir" | |
echo "Done" | |
} | |
# for each user, try to change their preferences | |
/usr/bin/dscl . list /Users UniqueID | /usr/bin/awk '{if($2>500){print$1}}' | while read -r username | |
do | |
printf "Setting JDK7 as highest-priority for existing user $username : " >&2 | |
homedir="`/usr/bin/dscl . read /Users/$username NFSHomeDirectory | /usr/bin/cut -d' ' -f2-`" | |
setvm "$homedir" | |
done | |
# also change the default preferences | |
/usr/bin/sudo /usr/bin/find '/System/Library/User Template' -type d -name '*.lproj' -depth 1 | while read -r language | |
do | |
printf "Setting JDK7 as highest-priority for new users, language $language : " >&2 | |
setvm "$language" | |
done |
This comment has been minimized.
This comment has been minimized.
Works with 10.7.0+, 10.8.0+. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Tested, it does the same as the control panel.