public
Last active

Automate the install of JDK 5 on Lion, Mountain Lion, Mavericks

  • Download Gist
install_jdk5_post_lion.sh
Shell
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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
#!/bin/bash
 
# This script is edited by Brice Dutheil
# See there in french http://blog.arkey.fr/2012/07/30/script-pour-installer-le-jdk-5-sur-macosx-lion/
# Translate button is broken for now, please use Google to translate this website.
#
# 2014/02/10 Updated the script to run on OSX 10.9 Maverick
#
# 2013/05/11 Added a few more guidance when Java Preferences is not available anymore
# Added a simple example of a JDK switch function.
#
# 2012/08/25 This script didn't behave correctly when ran on 10.8.1
# Added recommendation to always run this script after updates such as Java, XCode, OSX, etc.
#
# 2O12/07/29 Added Mountain Lion support => Choose the 64bit JVM !
# Can dowload the Java DMG itself if not present in same directory
# Colored the output a bit, works well on a black background
# Added tips for using the different JVMs
# Removed 32bit mode for Mountain Lion (Thanks to Henri Gomez for pointing me to 'ditto')
#
# 2011/12/04 Added warnings and some more information on gotchas
#
# 2011/08/25 Updated this very comments
#
# 2011/08/22 Initial version (Thanks to Benjamin Morin for his blog post)
# This script heavily inspired/copied from http://www.s-seven.net/java_15_lion
 
 
 
 
 
 
#some variables
javadmgurl='http://support.apple.com/downloads/DL1359/en_US/JavaForMacOSX10.5Update10.dmg'
javapkg='JavaForMacOSX10.5Update10'
jvmver='1.5.0_30'
jvms_path='/System/Library/Java/JavaVirtualMachines'
java_frmwk_path='/System/Library/Frameworks/JavaVM.framework/Versions'
pushd `dirname $0` > /dev/null
script_location=`pwd -P`
popd > /dev/null
 
# locate Java Preferences in /Applications
java_prefs=`mdfind -onlyin /Applications "Java Preferences.app"`
 
 
 
declare "osxname_14=OS 11"
declare "osxname_13=Mavericks"
declare "osxname_12=Mountain Lion"
declare "osxname_11=Lion"
 
get_osx_name() {
local array="osxname" key=$1
local declare_name="${array}_${key}"
printf '%s' "${!declare_name}"
}
 
# 13.0.0 = Mavericks = 10.9.0
# 12.1.0 = Mountain Lion = 10.8.1
# 12.0.0 = Mountain Lion = 10.8
# 11.0.0 = Lion = 10.7
 
darwin_version=`uname -r`
osx_version=`sw_vers -productVersion`
osx_commercial_name=$(get_osx_name ${darwin_version%.[0-9].[0-9]})
test ${darwin_version/1[2-9]./} != $darwin_version && is_mountain_lion=true
 
 
 
# colors and style
RESET=`tput sgr0`
RED=`tput setaf 1`
BLUE=`tput setaf 4`
PURPLE=`tput setaf 5`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
UNDERLINED=`tput smul`
BOLD=`tput bold`
 
 
 
# escape aware echo
echo() { builtin echo -e $@; }
 
 
 
# Make sure only root can run the script
if [ $EUID -ne 0 ]; then
echo $RED'This script must be run as root in order to install the JDK! If unsure why check the script.'$RESET 1>&2
exit 1
fi
 
 
 
 
# Make sure the user understand he is all alone if something goes wrong
if [ $is_mountain_lion ]; then
echo $BLUE'==>'$RESET' You are using '$BOLD$osx_commercial_name$RESET', the script has been updated to work, however
'$osx_commercial_name' kernel works in 64bit. This shouldn'"'"'t be an issue, as event the JDK 6 32bit is working,
however 32bit mode doesn'"'"'t work for this hacky install of JDK 5. '$YELLOW'It means that only
the 64bit version of the JDK5 will work on your OS.'$RESET
echo
fi
echo $BLUE'==>'$RESET' The present script has been tested on my current setup and is far from
bulletproof, it might not work at all on your system. And there is '$RED'*no
uninstall script*'$RESET' for now!'
echo
echo $BLUE'==>'$RESET' Again '$RED'this script touches system files'$RESET', please be advised you are the sole
responsible to run or '$BOLD'TO NOT'$RESET' run this script on your machine.'
echo
 
 
# Reminder about Apple JDK updates
echo $YELLOW$UNDERLINED'NOTES :'$RESET
echo $BLUE'==>'$RESET' Generally speaking it seems that '$YELLOW'applying updates'$RESET' on your system Java, XCode, OSX, etc.
might cause problems with your current install, '$RED'reapply this script after any update if you
experience issues with your Java 5 install.'$RESET
echo $BLUE'==>'$RESET' When '$YELLOW'applying a Java update from Apple'$RESET', some important
symbolic names that refer to this install are resetted to factory default
values, you can just re-apply this script.'$RESET
echo
if [[ -n "$java_prefs" && $is_mountain_lion ]]; then
echo $BLUE'==>'$RESET' For people that where upgrading OS X, it seems this scripts fail to open
Java Preferences app at the end of the script, with an error like that:'
echo $PURPLE'\tLSOpenURLsWithRole() failed with error -10810 for the application /Applications/Utilities/Java Preferences.app.'
echo $PURPLE'\tFSPathMakeRef(/Applications/Utilities/Java Preferences.app) failed with error -43.'
echo
echo $YELLOW' If this is happening, '$RED'you have to (re)install Java 6 !'
echo $YELLOW' You can enter these commands yourself in root mode :'
echo $YELLOW'\tsudo rm -rf /System/Library/Java/JavaVirtualMachines/1.6.0.jdk'
echo '\tsudo rm -rf /System/Library/Java/JavaVirtualMachines'
echo '\tjava'
echo
echo $RESET' This last command will trigger the Java 6 install, then you can run again this script.'$RESET
echo
fi
 
printf "%s " 'Do you still want to proceed ? (y/n)'
read answer
[ $answer != 'y' ] && echo 'You'"'"'re fine, JDK5 Hacky Install script has been aborted' && exit 1
echo
 
 
 
echo
echo $UNDERLINED'Here we go...'$RESET
# ===================================
echo
 
if [ ! -f $javapkg.dmg ]; then
echo 'The "Java for Mac OS X 10.5 Update 10" DMG ('"$javapkg.dmg"') was not found locally.'
echo 'Now trying to download the DMG file from Apple website (http://support.apple.com/kb/DL1359).'
echo $javadmgurl' -> '$script_location/$javapkg.dmg
echo -n $BLUE
curl -C - -# -L $javadmgurl -o $script_location/$javapkg.dmg
echo -n $RESET
 
if [ ! -f $script_location/$javapkg.dmg ]; then
echo 'Couldn'"'"'t download the uptate. Please download it from Apple at :
http://support.apple.com/kb/DL1359'
echo 'And place it in the same folder as this script : '$script_location
exit 1
fi
else
echo 'Using '$javapkg'.dmg file to install "Java for Mac OS X 10.5 Update 10".'
fi
 
 
 
 
 
# Extracting the DMG content in temporary location
echo
echo 'Extracting Java for Mac OS X package'
mkdir /tmp/jdk5dmg
hdiutil attach -quiet -nobrowse -mountpoint /tmp/jdk5dmg/ $script_location/$javapkg.dmg
cd /tmp/jdk5dmg/
# too bad pkgutil nor xar cannot stream package content
pkgutil --expand $javapkg.pkg /tmp/jdk5pkg
 
cd ..
hdiutil detach -quiet -force /tmp/jdk5dmg/
rm -rf /tmp/jdk5dmg/
 
 
 
 
# Prepare the System JVM path
if [ ! -e $jvms_path ]; then
echo 'Create '$jvms_path', as it does not exist on your system (it might be because
you don'"'"' t have another Java install).'
mkdir -v -p $jvms_path
fi
 
echo
echo 'Removing previous Java 1.5 file / directory or symbolic links in :'
cd $jvms_path
pwd
rm -rf 1.5
rm -rf 1.5.0
cd $java_frmwk_path
pwd
rm 1.5/ > /dev/null 2>&1 || rm -rf 1.5 > /dev/null 2>&1
rm 1.5.0/ > /dev/null 2>&1 || rm -rf 1.5.0 > /dev/null 2>&1
rm -rf $jvmver 2>&1
 
 
 
 
echo
echo $UNDERLINED'Preparing JavaVM framework'$RESET
# ================================================
echo
 
echo 'Extracting JDK 1.5.0 from package payload in :'
cd $java_frmwk_path
pwd
gzip -cd /tmp/jdk5pkg/$javapkg.pkg/Payload | pax -r -s \
',.'$java_frmwk_path'/1.5.0,./'$jvmver',' \
'.'$java_frmwk_path'/1.5.0'
ls -Fld 1.5*
 
rm -rf /tmp/jdk5pkg/
 
echo
echo 'Recreating symbolic links to ./'"$jvmver"' for 1.5 and 1.5.0 :'
pwd
ln -svhF ./$jvmver 1.5
ln -svhF ./$jvmver 1.5.0
 
echo
echo 'Changing values in config files to make JDK work with '$osx_commercial_name
cd $jvmver
/usr/libexec/PlistBuddy -c "Set :JavaVM:JVMMaximumFrameworkVersion 14.*.*" ./Resources/Info.plist
/usr/libexec/PlistBuddy -c "Set :JavaVM:JVMMaximumSystemVersion "$osx_version".*" ./Resources/Info.plist
/usr/libexec/PlistBuddy -c "Add :CFBundleExecutable string libjava.jnilib" ./Resources/Info.plist
ln -siv ./Resources/Info.plist .
 
echo
echo 'Linking Apple native wrapper'
mkdir ./MacOS
ln -siv ../Libraries/libjava.jnilib ./MacOS
 
 
 
 
echo
echo $UNDERLINED'Preparing Java Virtual Machine'$RESET
# ====================================================
cd $jvms_path
mkdir -v 1.5.0
cd 1.5.0
pwd
ln -sivh $java_frmwk_path/$jvmver ./Contents
 
 
if [ $is_mountain_lion ]; then
echo
echo $BOLD'REMINDER'$RESET' : You are using '$osx_commercial_name' which is running a '$BOLD'64 bit kernel'$RESET', this causes segfaults
when the Java 5 JVM is run in 32 bit mode. For this reason this script removes 32bit mode on this JVM.'$RESET
 
ditto --arch x86_64 $java_frmwk_path/$jvmver $java_frmwk_path/$jvmver-x64
rm -rf $java_frmwk_path/$jvmver
mv $java_frmwk_path/$jvmver-x64 $java_frmwk_path/$jvmver
fi
 
 
 
 
echo
echo $UNDERLINED'Almost over...'$RESET
# ====================================
echo
 
# opening Java Preferences
if [ -n "$java_prefs" ]; then
# open -a "/Applications/Utilities/Java Preferences.app"
echo $BLUE'==> TIP'$YELLOW' : If you are using applications that need Java 6, but some other command line apps that require JDK 5 :'
echo ' - keep the "Java SE 6" entry at the top in "Java Preferences"'
echo ' - use the Apple "/usr/libexec/java_home" tool, for example to choose the "J2SE 5.0 64-bit" version :'
echo $PURPLE'\texport JAVA_HOME=`/usr/libexec/java_home -F -v 1.5 -a x86_64 -d64`'$RESET
echo
 
echo 'Now check that JDK 5 appears in Java Preference App, if yes the install is successful, otherwise
try asking the internet :-/'
 
open -a "$java_prefs"
else
echo $RED'This script could not find the Java Preferences, maybe you moved it elsewhere, or maybe you are running
a recent version of MacOSX.'$RESET
 
echo 'In recents MacOSX, Apple decided to remove the Java Preference app, which means you
cannot reorder the JDK Preferences, hence you cannot choose JDK5 as a defaukt JDK for the whole OS,
you can only specify it in the terminal via the $PATH variable.'
echo
echo 'Check that /usr/libexec/java_home knows about JDK5, other wise try asking the internet :-/'
fi
 
echo '(starting here : '$YELLOW'https://gist.github.com/1163008#comments'$RESET')'
echo
 
echo
echo $UNDERLINED'/usr/libexec/java_home says :'$RESET
# ===================================================
# listing JVMs on local machine
echo $(/usr/libexec/java_home -V 2>&1 | sed -E 's,$,\\n,g' | sed -E 's,.*J2SE 5.0.*,\\033[0;33m&\\033[00m,')
 
echo $UNDERLINED'java -version says :'$RESET
echo $(/usr/libexec/java_home -F -v 1.5 -a x86_64 --exec java -version 2>&1 | sed -E 's,$,\\n,g' | sed -E 's,java version.*,\\033[0;33m&\\033[00m,')
echo
 
echo $UNDERLINED'You can also try the java 5 command yourself'$RESET
# ==================================================================
# possible commands
echo $GREEN'\t/usr/libexec/java_home -F -v 1.5 -a x86_64 --exec java -version'$RESET
echo $GREEN'\t`/usr/libexec/java_home -F -v 1.5 -a x86_64`/bin/java -version'$RESET
echo
echo 'Don'"'"'forget to update the JAVA_HOME accordingly!'
echo
 
 
printf "%s" 'Yeah I got it ! (Press Enter) '
read -s -n 0 key_press
echo
 
echo
echo
echo $BLUE'==> TIP'$RESET' : To switch the JDK version in your shell you can use the great '$BOLD'jenv'$RESET' project ('$YELLOW'https://github.com/gcuisinier/jenv'$RESET'),
that can easily switch your JDK version globally or per shell or folder.'
echo 'jenv pretty much covers for the terminal what the Java Preference app did, but in much usable way for terminal users.'
echo
echo 'The old school way would be to change the java runtime with the correct runtime path in $JAVA_HOME and $PATH'
echo 'Below is a simple function that you can put in your shell rc (.bashrc, .zshrc, ...) that automates steps
to switch the JDK to the wanted version. Adapt it to your needs. It uses the Apple tool : /usr/libexec/java_home'
echo
# /System/Library/Java/JavaVirtualMachines/1.5.0/Contents/Home/bin
# /Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home/bin
echo $GREEN'\tfunction switch_jdk() {'
echo '\t\tlocal wanted_java_version=$1'
echo '\t\texport JAVA_HOME=`/usr/libexec/java_home -F -v $wanted_java_version -a x86_64 -d64`'
echo '\t'
echo '\t\t# cleaned PATH'
echo '\t\texport PATH=$(echo $PATH | sed -E "s,(/System)?/Library/Java/JavaVirtualMachines/[a-zA-Z0-9._]+/Contents/Home/bin:,,g")'
echo '\t'
echo '\t\t# prepend wanted JAVA_HOME'
echo '\t\texport PATH=$JAVA_HOME/bin:$PATH'
echo '\t'
echo '\t\techo "Now using : "'
echo '\t\tjava -version'
echo '\t}'$RESET
echo
 
echo 'And just use it this way :'
echo $GREEN'\tswitch_jdk 1.5'$RESET
echo $GREEN'\tswitch_jdk 1.7.0_45'$RESET
echo $GREEN'\tswitch_jdk 1.7.0_51'$RESET
 
 
 
echo
echo $RESET'.'

This worked great. Thanks a lot.

Thanks, Script worked great.

Hi there, i don't have a lot of experience with macs sorry but how do you change the values when in Terminal. Cheers.

Hi, actually the script don't ask for values when run. It merely ask if you are sure to perform these actions.
What do you want to change ?

Thanks for the handy script. I used this script in Lion and it worked like a charm. I tried this in Mountain Lion and it worked with one minor change. After upgrading to ML, the directory "/System/Library/Java/JavaVirtualMachines" is removed from the system. I had to manually create this directory before running the script. It would be useful if the script can do a sanity check and create this dir if needed. Although, I can use the Java 5 just fine, the Java Preferences App does not show this version. Not sure what has changed in ML preventing it to show up in the Java Preferences App.

Hi Arul,

OK, I haven't tried it Java on Mountain Lion, but as Apple is transferring the Java platform ownership to Oracle, I guess this location in the System path is not anymore relevant to the Java Preferences App.

I'll update the gist to add a warning message for Mountain Lion owners !

Thanks for the report :)

Hi all,
I've added Mountain Lion support, only the 64bit JVM is working though. The 32bit JVM is segfaulting.

This script is for Lion and Moutain Lion but should works with Snow also ?

It might work, the instructions looks similar (http://chxor.chxo.com/post/183013153/installing-java-1-5-on-snow-leopard). However I never tried it as I don't have a Snow Leopard at reach.

I ran the updated script, it failed to launch the Java Preferences App after install, here is error I am seeing in ML:
Yeah I got it ! (Press Enter)
Now check that JDK 5 appears in Java Preference App, if yes the install is successful, otherwise try asking the internet :/

LSOpenURLsWithRole() failed with error -10810 for the file /Applications/Utilities/Java Preferences.app.

This is happening because of the following entry in /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0_30/Resources/Info.plist:

           <key>JVMMaximumSystemVersion</key>
            <string>10.8.*</string>

If I change it back to 10.7, the Java Preferences App launches fine, but it does not detect Java 5.

@aruld
Hmm this crash is wierd. Of course this value is necessary to show up Java Preference.
I have a clean Mountain Lion install and ML downloaded Java 6 when an application required it. Then I worked on this script.
Did you updated from Lion to Mountain Lion ? Did you tried to lauch Java Preferences manually ?

What do you see when you execute /usr/libexec/java_home -V ?
And `/usr/libexec/java_home -F -v 1.5 -a x86_64 -d64`/bin/java -version ?

Also do you see the same error with command ?
open "/Applications/Utilities/Java Preferences.app"

And with this one :
open -a "/Applications/Utilities/Java Preferences.app"

I believe I got it working now and thanks for that tip which did the trick on my system (ML downloaded Java 6 when an application required it). I believe this step is required for your script to launch Java Preference app successfully. I upgraded from Lion to Mountain Lion. All my Java installs under /System/Library/Java were wiped out completely. And it looks like I was never able to manually trigger ML download Java 6 until I used a Java 5 app that required it. Once the Java 6 was installed under System, I re-ran your script and it worked like a charm!

@aruld ok, cool, I'll update the script with this warning :)

Here, Java Preferences.app fails to open with a 'LSOpenURLsWithRole() failed for the application /Applications/Utilities/Java Preferences.app with error -10810' error

Got it working by deleting /System/Library/Java/JavaVirtualMachines/1.6.0.jdk and /System/Library/Java/JavaVirtualMachines, then typing java from the terminal to force java6 reinstall, then run your script. And voila, it works now.

@dgageot Thx David, I added instructions for that in the script. I don't really want to perform these actions automatically at the moment.

I got this error too after installing Java7 (JDK) yesterday. It seems that the Java control panel was replaced with Java7 with a non osx native dialog (actually, in Java).

To clearify: I have 10.8, I had install JDK 7 and I used the script to install JDK5. The output from the command line is

Matching Java Virtual Machines (4):
    1.6.0_31-b04-415, x86_64:   "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0_31-b04-415.jdk/Contents/Home
    1.6.0_31-b04-415, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0_31-b04-415.jdk/Contents/Home
    1.7.0_06, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_06.jdk/Contents/Home
    1.5.0_30-b03, x86_64:   "J2SE 5.0"  /System/Library/Java/JavaVirtualMachines/1.5.0/Contents/Home

/Library/Java/JavaVirtualMachines/1.6.0_31-b04-415.jdk/Contents/Home

Fine. But the GUI (from Java 7) won't show more than Java 7 itself. However, that's no problem of this script.

I installed the Jdk with your script on montain lion (I already used it on lion).
It succeed in installing the Jdk (including the JavaPrefs).
But I don't succeed in running 'java -version', but I'm able to do a maven build with the jdk5...

pke:$ java -version
java version "1.6.0_33"
Java(TM) SE Runtime Environment (build 1.6.0_33-b03-424-11M3720)
Java HotSpot(TM) 64-Bit Server VM (build 20.8-b03-424, mixed mode)
pke:$ export JAVA_HOME=/usr/libexec/java_home -F -v 1.5 -a x86_64 -d64
pke:$ java -version
^C

I need to kill the java process that consume 100% of CPU and that never stop.

pke:$ mvn -v
Cannot run Java in 32 bit mode. Continuing in 64 bit mode.
Apache Maven 3.0.3 (r1075438; 2011-02-28 18:31:09+0100)
Maven home: /usr/share/maven
Java version: 1.5.0_30, vendor: Apple Inc.
Java home: /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.8", arch: "x86_64", family: "unix"

An idea of what's wrong ?

pke:$ /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
1.6.0_33-b03-424, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_33-b03-424, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.5.0_30-b03, x86_64: "J2SE 5.0" /System/Library/Java/JavaVirtualMachines/1.5.0/Contents/Home

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

@knalli That's weird I don't have these issues, that should be worth investigating. Did you try to reinstall all your Java installs, then reinstall the default JDK 6, then JDK 7, then use this script ?

@pkernevez Did you updated to Mountain Lion 10.8.1 ?

@pkernevez Actually I think you are misusing the command line :)

java_home tells you that the current JDK is : /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

So when you run these commands :

export JAVA_HOME=`/usr/libexec/java_home -F -v 1.5 -a x86_64 -d64` # indeed stores the java 5 path in JAVA_HOME
java -version # still referencing the default JDK in Java Preferences.app, so java of Java SE 6 is used with a JAVA_HOME set to the JDK 5, which causes trouble

It probably means that you are running the /usr/bin/java executable, this one is using the JVM you defined in Java Preferences.

What you need to do instead, like in any multiple java install (on windows, linux as well) is to use the actual path of the JVM you want :

/usr/libexec/java_home -F -v 1.5 -a x86_64 --exec java -version
`/usr/libexec/java_home -F -v 1.5 -a x86_64`/bin/java -version
$JAVA_HOME/bin/java -version

Alternatively you can prefix the $PATH variable JDK path you want in your current session or script :

export PATH=$JAVA_HOME/bin:$PATH
which java
java -version

i'm having the same issue as @pkernevez.

i can run the separate java's fine if i specify the full paths.

but if i just run plain "java" (/usr/bin/java) it pegs the cpu at 100% until i kill it.

which breaks third-party tools that assume the default java in the path will work (like rvm).

@trak3r Did you checked your JAVA_HOME ?

@trak3r Also if you updated something in OSX, Java, or something else, you might have to reapply this script, otherwise you might have issue such as the one you encounter. The script is warning you about that beforehand.

Hello
I did execute the script but the JavaPreferences app do not run any longer ...
I receive a dialog box saying "no Java 1.5+ copatible is available

and look at the return of that command
Gmac:TICS gh$ /usr/libexec/java_home
Unable to find any JVMs matching version "(null)".

here is the full output of the script :

Gmac:TICS gh$ chmod +x install_jdk5_lion.sh
Gmac:TICS gh$ !sudo
sudo install_jdk5_lion.sh
=> You are using Mountain Lion, the script has been updated to work, however Mountain Lion kernel works in 64bit. This shouldn't be an issue, as the JDK 6 32bit is working, however it actually doesn't work for this hacky install of JDK 5. It means that only the 64bit version will work on your OS.

=> The present script has been tested on my current setup and is far from bulletproof, it might not work at all on your system. And there is no uninstall script for now!

=> Again this script touches system files, please be advised you are the sole responsible to run or TO NOT run this script on your machine.

NOTES :
=> Generally speaking it seems that applying updates on your system Java, XCode, OSX, etc. might cause problems with your current install, reapply this script after any update if you experience issues with your Java 5 install.

=> When applying a Java update from Apple, some important symbolic names that refer to this install are resetted to factory default values, you can just re-apply this script.

=> For people that where upgrading OS X, it seems this scripts fail to open Java Preferences at the end of the script, with an error like that:
LSOpenURLsWithRole() failed with error -10810 for the application /Applications/Utilities/Java Preferences.app.

If this is happening, you have to (re)install Java 6 !
You can enter these commands yourself in root mode :
sudo rm -rf /System/Library/Java/JavaVirtualMachines/1.6.0.jdk
sudo rm -rf /System/Library/Java/JavaVirtualMachines
java
This last command will trigger the Java 6 install, then you can run again this script.

Do you still want to proceed ? (y/n)y

Here we go

Using JavaForMacOSX10.5Update10.dmg file as the "Java for Mac OS X 10.5 Update 10".

Extracting Java for Mac OS X package
Could not open package for expansion: JavaForMacOSX10.5Update10.pkg
Create /System/Library/Java/JavaVirtualMachines, as it does not exist on your system (it might be because you don' t have another Java install)

Removing previous Java 1.5 file / directory or symbolic links in :
/System/Library/Java/JavaVirtualMachines
/System/Library/Frameworks/JavaVM.framework/Versions

Preparing JavaVM framework

Extracting JDK 1.5.0 from package payload in :
/System/Library/Frameworks/JavaVM.framework/Versions
gzip: /tmp/jdk5pkg/JavaForMacOSX10.5Update10.pkg/Payload.gz: No such file or directory
pax: End of archive volume 1 reached
pax: Sorry, unable to determine archive format.
ls: 1.5*: No such file or directory

Recreating symbolic links to ./1.5.0_30 for 1.5 and 1.5.0 :
/System/Library/Frameworks/JavaVM.framework/Versions
1.5 -> ./1.5.0_30
1.5.0 -> ./1.5.0_30

Changing values in config files to make JDK work with Lion / Mountain Lion
./install_jdk5_lion.sh: line 203: cd: 1.5.0_30: No such file or directory
File Doesn't Exist, Will Create: ./Resources/Info.plist
Set: Entry, ":JavaVM:JVMMaximumFrameworkVersion", Does Not Exist
File Doesn't Exist, Will Create: ./Resources/Info.plist
Set: Entry, ":JavaVM:JVMMaximumSystemVersion", Does Not Exist
File Doesn't Exist, Will Create: ./Resources/Info.plist
./Info.plist -> ./Resources/Info.plist

Linking Apple native wrapper
./MacOS/libjava.jnilib -> ../Libraries/libjava.jnilib

Preparing Java Virtual Machine
mkdir: created directory '1.5.0'
/System/Library/Java/JavaVirtualMachines/1.5.0
./Contents -> /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0_30

REMINDER : You are using Mountain Lion which is running a 64 bit kernel, this causes segfaults when the Java 5 JVM is run in 32 bit mode. For this reason this script removes 32bit mode on this JVM.
ditto: can't get real path for source
mv: rename /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0_30-x64 to /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0_30: No such file or directory

Almost over...

TIP : If you are using applications that need Java 6, but some other command line apps that require JDK 5 :

  • keep the "Java SE 6" entry at the top in "Java Preferences"
  • use the Apple "/usr/libexec/java_home" tool, for example to choose the "J2SE 5.0 64-bit" version : export JAVA_HOME=/usr/libexec/java_home -F -v 1.5 -a x86_64 -d64

Yeah I got it ! (Press Enter)
Now check that JDK 5 appears in Java Preference App, if yes the install is successful, otherwise try asking the internet :-/
(starting here : https://gist.github.com/1163008#comments)

/usr/libexec/java_home says :
Matching Java Virtual Machines (2):
1.6.0_29-b11-402, x86_64: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0_29-b11-402.jdk/Contents/Home
1.6.0_29-b11-402, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0_29-b11-402.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/1.6.0_29-b11-402.jdk/Contents/Home

You can also try the java 5 command yourself
/usr/libexec/java_home -F -v 1.5 -a x86_64 --exec java -version
/usr/libexec/java_home -F -v 1.5 -a x86_64/bin/java -version
Gmac:TICS gh$ java
-bash: /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0_30/home/bin/java: No such file or directory
Gmac:TICS gh$

Please Help I am getting nuts

@Chewbee I never experienced this error, did you tried to reinstall java 6, and see first if it's working better with Java Preferences, then try to reinstall java 5

Yes, I redo everything today :

1.6 >> Install this package http://support.apple.com/kb/DL1572?viewlocale=en_US
1.5 >> Replay this script https://gist.github.com/1163008

At the end I have no failure in those processes and the java panel opens and displays the Java versions ..

However Lotus Notes no longer works :

mac:MacOS gh$ !500
/Applications/Notes.app/Contents/MacOS/Notes &
[1] 520
Gmac:MacOS gh$ IBM Lotus Notes:
A Java Runtime Environment (JRE) or Java Development Kit (JDK)
must be available in order to run IBM Lotus Notes. No Java virtual machine
was found after searching the following locations:
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Libraries/libjvm.dylib
Gmac:MacOS gh$
[1]+ Exit 1 /Applications/Notes.app/Contents/MacOS/Notes

The funniest part beiing :

Gmac:MacOS gh$ java -version
java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-11M3811)
Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)
Gmac:MacOS gh$

Go figure ;) what is going on ???

Of course I need to have those two version at the same time

I don't use Lotus Notes, but the mentioned path doesn't exist anymore on Lion / Mountain Lion, as Apple is cleaning up his Java implementation. And Lotus Notes is probably not updated to take care of such changes. The CurrentJDK symbolic link doesn't exist anymore in this location.
For exemple when the JDK5 is installed with my script the following path exists : /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0_30/Libraries/libjvm.dylib

So you'll probably need to figure out a way to tell Lotus to use this JDK 5 instead of the system wide Java. The first thing I would like to try is to create a CurrentJDK symbolic link to 1.5.0_30 or to Current

Yeah I tried that but the directory structure is different ...
/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0_30/Libraries/ does not exists in JRE6
To be clear

Gmac:Current gh$ pwd
/System/Library/Frameworks/JavaVM.framework/Versions/Current
Gmac:Current gh$ ls -alF
total 72
drwxr-xr-x 7 root wheel 238 24 sep 08:27 ./
drwxr-xr-x 13 root wheel 442 24 sep 08:29 ../
drwxr-xr-x 43 root wheel 1462 24 sep 08:27 Commands/
drwxr-xr-x 4 root wheel 136 19 aoĆ» 01:17 Frameworks/
-rwxr-xr-x 1 root wheel 103472 24 sep 08:27 JavaVM*
drwxr-xr-x 43 root wheel 1462 24 sep 08:27 Resources/
drwxr-xr-x 3 root wheel 102 24 sep 08:27 _CodeSignature/

were in JDK 5

Gmac:1.5 gh$ pwd
/System/Library/Frameworks/JavaVM.framework/Versions/1.5
Gmac:1.5 gh$ ls -alF
total 8
drwxr-xr-x 9 root wheel 306 24 sep 08:28 ./
drwxr-xr-x 13 root wheel 442 24 sep 08:29 ../
drwxr-xr-x 10 root wheel 340 22 jui 2011 Classes/
drwxr-xr-x 35 root wheel 1190 22 jui 2011 Commands/
drwxr-xr-x 8 root wheel 272 22 jui 2011 Home/
lrwxr-xr-x 1 root wheel 22 24 sep 08:29 Info.plist@ -> ./Resources/Info.plist
drwxr-xr-x 42 root wheel 1428 22 jui 2011 Libraries/
drwxr-xr-x 3 root wheel 102 24 sep 08:28 MacOS/
drwxr-xr-x 24 root wheel 816 24 mai 2011 Resources/

So I am a bit lost because Lotus Notes needs the 1.6 to run in full

Well the way to switch Java versions (especially with java 5) and the way the applications use this might be somehow not compatible.

Also weirdly, I noticed some inconsistencies on the Apple side, where there is the Current symlink and sometime but not always CurrentJDK, CurrentJDK can also disappear when switching the JDK.

Also in my previous posts I though Lotus Notes needed Java 5. I only use Java 5 from the command line, where I can specify actual paths, applications should always use the default Java in my opinion. So if your Java Preferences is already set to Java 6, you should probably create a CurrentJDK symlink pointing to Current or to the path of Java 6.

Thanks again for your help, I hope you are not bothered by those Lotus Notes Annoyances ...

On my system I do not have a CurrentJDK linking it to current was my first guess, and I thought it worked in the past but no longer do
drwxr-xr-x 10 root wheel 340 24 sep 08:27 ../
lrwxr-xr-x 1 root wheel 10 24 sep 08:27 1.4@ -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 24 sep 08:27 1.4.2@ -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 24 sep 08:28 1.5@ -> ./1.5.0_30
lrwxr-xr-x 1 root wheel 10 24 sep 08:28 1.5.0@ -> ./1.5.0_30
drwxr-xr-x 9 root wheel 306 24 sep 08:28 1.5.0_30/
lrwxr-xr-x 1 root wheel 10 24 sep 08:27 1.6@ -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 24 sep 08:27 1.6.0@ -> CurrentJDK
drwxr-xr-x 7 root wheel 238 24 sep 08:27 A/
lrwxr-xr-x 1 root wheel 1 24 sep 08:27 Current@ -> A
lrwxr-xr-x 1 root wheel 7 24 sep 13:04 CurrentJDK@ -> Current
lrwxr-xr-x 1 root wheel 22 19 sep 10:19 Info.plist@ -> ./Resources/Info.plist
drwxr-xr-x 3 root wheel 102 19 sep 10:19 MacOS/
Gmac:Versions gh$ !/App
/Applications/Notes.app/Contents/MacOS/Notes
Gmac:Versions gh$ /Applications/Notes.app/Contents/MacOS/Notes
IBM Lotus Notes:
A Java Runtime Environment (JRE) or Java Development Kit (JDK)
must be available in order to run IBM Lotus Notes. No Java virtual machine
was found after searching the following locations:
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Libraries/libjvm.dylib
Gmac:Versions gh$

Unfortunately ...

Weird, I'm really not sure what's happening here. What is the output of your java_home tool ?

It's a little work, but it might be necessary in your case, if you remove every Java install ; then if you install the default Java only (java 6 shipped by Apple), is Lotus Notes working ? If yes proceed step-by-step to see if Lotus Notes is still working when you install other Java VMs, also monitor what's happening in this directory /System/Library/Frameworks/JavaVM.framework/Versions.

Another suggestion, might be to look at Lotus Notes config files, I don't know this application, but there might be some way to configure which Java Home is used (in an Info.plist maybe).

Hello,
At the end of the day I found something !!

I reinstalled everything I.e. Java 6 (JDK) Java 7 ( JDK) , Lotus Notes ... Nothing

Then I did a find / -name libjvm.dylib -print &> findError.txt and found that Gmac:~ gh$ cat findError.txt | grep -i libjvm.dylib
/Applications/Xcode.app/Contents/Applications/Application Loader.app/Contents/MacOS/itms/java/lib/client/libjvm.dylib
/Applications/Xcode.app/Contents/Applications/Application Loader.app/Contents/MacOS/itms/java/lib/server/libjvm.dylib
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/server/libjvm.dylib
/Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home/jre/lib/server/libjvm.dylib
/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0_30/Libraries/libjvm.dylib
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Libraries/libjvm.dylib
/Users/gh/.Trash/1.6.0_29-b11-402.jdk/Contents/Libraries/libjvm.dylib
/Users/gh/.Trash/Developer/Applications/Utilities/Application Loader.app/Contents/MacOS/itms/java/lib/client/libjvm.dylib
/Users/gh/.Trash/Developer/Applications/Utilities/Application Loader.app/Contents/MacOS/itms/java/lib/server/libjvm.dylib
/Users/gh/.Trash/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Libraries/libjvm.dylib
Gmac:~ gh$

So I did udo ln -siv /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents CurrentJDK
CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents

Et Voila !! ....

I can say I am a dumbass because I should have made this a long time ago but at the end of the day it works, and I was willing to share this here as I polluted this thread comments a lot so I have to show that this is solved for anyone experienceing the same pitfalls ...

I have to add regarding your last answer that the regular java command were working so Java was properly installed on the path but the lack of a proper CurrentJDK seems to be enough to lost Lotus Notes ...

As to choose what Java to force Notes to use I never found out how to do that

Anyway thanks a lot for you help and sorry for the thread pollution

Cool that you solved your current issue with Lotus Notes. Still Apple / Oracle Java install conventions switch made a few things inconsistent on the Current / CurrentJDK symbolic links.

I'll probably need to warn users about that inconcistency one day or another.

Thanks for the update anyway :)

Seems like the Java Mayhem still continues with Apple and Oracle, in the latest Java 6 update, they removed the Java Preferences application, leaving only the Oracle one, which doesn't show all JDKs you can have, java_home still lists them but doesn't allow to define a default JDK.

http://reviews.cnet.com/8301-13727_7-57533880-263/java-preferences-missing-after-latest-os-x-java-update/

Hi, this script worked for Java 1.5. But now it does not show up in Eclipse for Execution Environments. Why do the Apple and Oracle Java have a .jdk-extension, 1.5.0 not?

$ ls -1 /Library/Java/JavaVirtualMachines/ /System/Library/Java/JavaVirtualMachines/
jdk1.7.0_07.jdk
jdk1.7.0_11.jdk
1.5.0
1.6.0.jdk

@fabian-hahn Because Apple changed conventions over time, some symbolic names are not the same either, between 1.5 and 1.6. And as Apple do not want anymore to deliver it's own JDK more issues will arise between 1.7 and 1.6.

For example "Java Preferences" app was removed, and I'm not sure I can bet on the future of their java_home command line tool.

For eclipse you have to set it manually, that's what I have to do anyway for different version of Java with IntelliJ.

Dear people. Oficial Apple download JavaForMacOSX10.5Update10.dmg exists no more. Found it here https://www.macupdate.com/app/mac/13295/apple-java-for-mac-os-x-10.5
That switching jdk function is a beauty, thanks a lot.

Here's the update for OS X Mavericks (10.9): https://gist.github.com/hansenc/7234566

@hansenc Thx, I've updated recently my script to support Mavericks in a convenient way (or at least I believe so). I also added a few refinement and tips.

Bu the way the link in the script still works : http://support.apple.com/downloads/DL1359/en_US/JavaForMacOSX10.5Update10.dmg

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.