Pick of the Week - Feb 6: [View All]
Chax
Add useful new features to iChat
    
 Welcome to macosxhints Over 8,417 hints served!   Sunday February 12, 2006



 Easily switch the current default Java version
 Fri, Jan 27 '06 at 05:21AM • from: Anonymous
UNIX 
I recently downloaded and installed Apple's Java 1.5.0 update for Tiger. Unfortunately, even using the bundled Java Preferences app, I could find no easy way to change the version of Java used by the java and javac commands in Terminal. Since I develop and test Java apps on different JDK versions, this was a necessity for me.

So I wrote the following simple UNIX shell script to allow me to easily switch between the different Java versions I have installed. Simply copy the code below into a text editor like TextEdit, and make sure to save as plain text:
#!/bin/sh

cd /System/Library/Frameworks/JavaVM.framework/Versions

CURJDK="`readlink CurrentJDK`"
echo Current JDK version: $CURJDK

if [ "$1" == "" ]; then
echo Installed versions:
ls
exit
fi

VERFOUND=`ls | grep $1 | head -n 1`

if [ "$VERFOUND" != "$1" ]; then
BASE="`basename $0`"
echo Error: Could not change JDK-- version $1 not installed!
echo Run $BASE without arguments to see a list of installed versions.
exit 127
fi

echo You must now enter your Mac OS X password to change the JDK.
sudo ln -fhsv $1 CurrentJDK
Save this as setJDK (or anything else that tickles your fancy) then go into the terminal and type chmod +x setJDK in the directory where you saved the file (ideally somewhere on your path). Run it without arguments to see what versions of Java are installed. You can then set the active version by entering the version as the first command-line argument.


______________________________________________

 
Easily switch the current default Java version
20 comments | Create New Account
The following comments are owned by whoever posted them. macosxhints is not responsible for what they say

Do not do this!
Authored by: hayne on Fri, Jan 27 '06 at 07:35AM
To repeat my comment from the last time a "hint" appeared suggesting a script for changing the Java version by modifying the symbolic links:

Arrgh! Changing the symbolic links (as is done by this script) is the wrong way to do this. This has been strongly recommended against by Apple. The right way to switch between Java versions is to use the facilities provided in the "Java Preferences" utility (provided with the Java 5 release in a recent software update) for GUI apps, and to change the execution PATH for command-line Java apps.

This was all explained in my macosxhints forums writeup and in this recent hint.

Changing the symbolic links is definitely the wrong way to go.
------------------------

Note that the forums writeup referred to above supplies Bash functions for changing the Java version the correct way - by changing the PATH and JAVA_HOME environment variables.

I also note that Apple is on the verge of releasing a new Java update that will make Java 1.5 (aka Java 5) the default Java version.

[ Reply to This ]

Easily switch the current default Java version
Authored by: wilton on Fri, Jan 27 '06 at 07:42AM
Agreed, this method can lead to problems...

Cyberduck broke doing this, and the author seems to have had many users complaining of the same thing.

Will

[ Reply to This ]

Easily switch the current default Java version
Authored by: yanokwa on Fri, Jan 27 '06 at 09:13AM
If it's just a command line problem, why don't you make an alias? For example in zsh alias javac15='/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Command$ alias java15='/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Commands$

[ Reply to This ]

Hayne is not the last word...
Authored by: raider on Fri, Jan 27 '06 at 09:38AM
I just wish this thing would die.

Hayne seems to think that his word gospel and that we should all believe him because he wrote a forum post.

The WHOLE REASON THAT THEY ARE SYMBOLIC LINKS is so that you can change them. That is the purpose for using symbolic links... AND - you can ALWAYS change them BACK!

I am a Java developer. I have been using Java 1.5.x with OSX since the first day it was released in beta. I have been using it with modified symlinks since day one.

I use Eclipse, XCode, and several other database and development tools. In addition, I have several Java applications not related to development. Even the most important Java application on my system, Puzzle Pirates. :)

They all work just fine. My system has even been through at least one automated Java update since the beta - which worked just fine and had no problems with the symlinks.

There is no reason to panic and if you would like to try it, changing you symlinks is just fine. If you find it causes a problem for something you use - then change it back. It is that simple.

And would the naysayers please just stop reading the hints regarding changing your Java symlinks - because obviously some people WANT it regardless of what you say. Be happy that you authored a forum post, and that you have warned appropriately and let the rest of us get on with our hints and tips.

As with any of the MacOSXHints - they are *always* "use at your own risk"

[ Reply to This ]

Easily switch the current default Java version
Authored by: stewarsh on Fri, Jan 27 '06 at 11:04AM
I'd like to point out that Sun (who invented Java) does precisely the same thing in Solaris to allow the sysadmin to change which java the user gets by default. Which is why /usr/bin/java is a link and not a real binary.

UNIX systems often use links to allow multiple version support, and OS X is no different.

[ Reply to This ]

Easily switch the current default Java version
Authored by: aamann on Sat, Jan 28 '06 at 11:57AM
Easily? - I guess using the Java Preferences Utility (which is also the official method recommended by Apple) is much easier...

[ Reply to This ]

From our Sponsor...

User Functions
User:
Password:
Don't have an account? Register as a new user

From our sponsors...


What's New: No hints this weekend
COMMENTS last 48 hrs
•   A couple of ways to find iDisk upload prog... [+4]
•   Another way to add iSync support for certa...
•   A fix for Checkpoint SecureClient under 10...
•   Mount DVDs as ISO images to resolve mounti...
•   Install free tools to possibly recover med... [+2]
•   Play multiple podcasts automatically on th... [+2]
•   Set up a Belkin wireless print server
•   >Set up a Belkin wireless print server
•   Create special single-click-to-launch Dock...
•   Add file references to anyone's Address Bo... [+3]
•   Browser Wars #7: Which browser is your cur... [+4]
•   10.4: Move swap files to another drive [+2]
•   10.4: Reconnect to Jabber transports in iChat
•   Create custom menu styles in Toast 7
•   iWork '06 - Nice improvements to Apple's '...
•   Use a D-Link DWL-G122 USB WiFi Dongle
•   10.3: Print from Windows XP to a shared Ma...
•   Open a specific man page section via URL
•   Re-record iTunes protected AAC music to MP3
•   iTunes 4.5 plays nice with Fast User Switc...
•   Possibly recover from a freeze without a r...
•   Chax - Add new features to iChat
•   10.4: Automatically accept video chats in ...
•   10.4: How to prevent single user mode logins

ACTIVITY Last 7 days
•  Feb 11
•  Feb 10
•  Feb 09
•  Feb 08
•  Feb 07
•  Feb 06
•  Feb 05
•  Search any date

LINKS last 2 wks
No recent new links

What's New in the
macosxhints.com Forums
•   Number of library songs different from ipo...
themachines04 10:47 pm
•   can i run .exe files?
nick101 10:15 pm
•   PB15 access to desktop iMac over the inter...
trevor 10:10 pm
•   mis handled file problems
trevor 9:29 pm
•   Carbon Copy Cloner--"space taken&quot...
NovaScotian 9:26 pm
•   Looking for automated way to define a list...
noneloud 9:22 pm
•   Changing iTunes filenaming convention
dpmccabe 9:18 pm

The Editor's Corner...
Here are some of my (robg) other projects...

•  The Robservatory
My blog - Macs, tech, etc.
•  Mac OS X Power Hound
The best of Hints in print
•  Inside iWork
An iWork training DVD
•  Macworld
Column, features, online...

Hints by Topic
 

News from Macworld
•   Macworld Podcasts: Macworld Podcast #28: Geeks at sea
•   Macworld Podcasts: Macworld Podcast #27: MacMania Cruise
•   Feature: Upgrading RAM: FAQs
•   Apple sells 12 million videos; Showtime added to iTunes
•   Apple offers 1GB iPod nano for $149
•   Inside Intel: Updated Lab Tests and Analysis

Vote
Browser Wars #7: Which browser is your current favorite?
Camino
DeskBrowse
Firefox
Flock
iCab
Internet Explorer
Mozilla
Netscape
OmniWeb
Opera
Safari
Sea Monkey
Shiira
Sunrise Browser
Other?
Results
7107 votes | 54 comments