setJDK/setGrails

I spend a lot of time bouncing between different versions of JDKs and different versions of Grails. To make my life easier, I’ve got these files in my bin directory.

setJDK

#!/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
sudo ln -fhsv $1 Current

I found that setJDK code somewhere on the internet, but I’m not really sure where and I had trouble finding it again. So I thought I’d share it here.

To integrate SoyLatte Java (which you probably want to be using anyway), just create a symlink in /System/Library/Frameworks/JavaVM.framework/Versions that points to your SoyLatte install.

setGrails

I’ve modified that script a bit to allow me to modify the version of Grails to use. This script assumes that you have your Grails distributions stored in BINDIR, and you’ve added $BINDIR/grails/bin to PATH. Once that’s set up, this script works like a charm:

#!/bin/sh
 
cd $BINDIR
 
CURGRAILS="`readlink grails`"
echo Current Grails version: $CURGRAILS
 
if [ "$1" == "" ]; then
echo Installed versions:
echo `ls -1 | grep "grails-"`
exit
fi
 
VERFOUND=`ls -1 | grep $1 | head -n 1`
 
if [ "$VERFOUND" != "grails-$1" ]; then
BASE="`basename $0`"
echo Error: Could not change Grails-- version $1 not installed!
echo Run $BASE without arguments to see a list of installed versions.
exit 127 
fi
 
ln -fhsv "grails-$1" grails

Finger-Saving Tips

If you’re really hardcore, add these lines to ~/.profile:

alias sg=setGrails
alias sj=setJDK

No related posts.

This entry was posted in Code Samples. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • Fred

    Nice. Command line tooling just doesn’t seem to be hotness in the Java community, so tips like this don’t get shared too often, it seems to me. I like!

    • http://www.smokejumperit.com Robert Fischer

      Too many people are too dependent on IDEs. I find more power with less annoyance in the command line.

      But, y’know, I started my programming life pissing people off in perl (e.g.) instead of pissing people off on the JVM, so I have an intimacy with the command line I don’t find in a lot of Java or .Net developers.

  • http://gabe97330.blogspot.com Gabe Beged-Dov

    Hi Robert,

    A similar version that separates the “default” (relink) from setversion is described in this post:

    http://old.nabble.com/setgrails-version-%28bash%29-script-with-completion-td26410455.html

    Best regards,

    Gabe

  • http://nakkaya.com Nurullah Akkaya
  • http://gabe97330.blogspot.com Gabe Beged-Dov

    The setjdk script (http://docs.codehaus.org/display/ninja/setjdk) referenced in my grails-user post (see earlier comment) supports setting a default version as well as setting a per-process version. It also has command completion.

    Best regards,

    Gabe

  • http://blog.headius.com Charles Oliver Nutter

    Try Nick Sieger’s “pickjdk” script…I love it. I made some modifications to allow selecting by number in one command as well:

    http://gist.github.com/143128

  • http://naleid.com/blog Ted Naleid

    Nice! I did something similar a while ago where it switches both the groovy and grails symlinks with one command. I like to have them in sync and can never quite remember which dot version of groovy belongs with a grails version.

    Mine is more manual than this though because it can’t interrogate what’s actually installed in the directory, you need to manually add an alias pairing the particular groovy and grails versions together. Somethign I could automate with a little awk/sed magic and looking in the lib dir, but the pain hasn’t quite hit that level yet :).

    Thanks for sharing. I love seeing other shell tricks for coding.

  • http://gabe97330.blogspot.com Gabe Beged-Dov

    The problem with relinking is that it is global so you can’t have several concurrent shells using different versions of the framework. That’s where the distinction of setdefault vs. setversion is useful. Also very nice to have completion.

  • Categories