6

I'm using Mac OSX 10.8.5 with Oracle's Java 1.7 installed in addition to the mac's 1.6. I have my JAVA_HOME set and the JAVA_HOME/bin in the front of my path. When I run a grails compile from the command line I can see it's choosing the Java 1.6 instead of 1.7. How do I make the grails command-line choose the JDK I want?

➤ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home
➤ echo $PATH
/Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home/bin /usr/local/share/npm/bin /Users/kbrodhagen/bin /Users/kbrodhagen/.rvm/bin /usr/bin /bin /usr/sbin /sbin /usr/local/bin /opt/X11/bin /usr/local/git/bin
➤ set -x JAVA_OPTS "-showversion"
➤ grails compile
java version "1.6.0_51"
Java(TM) SE Runtime Environment (build 1.6.0_51-b11-457-11M4509)
Java HotSpot(TM) 64-Bit Server VM (build 20.51-b01-457, mixed mode)
kennbrodhagen
  • 4,258
  • 2
  • 26
  • 21

5 Answers5

3

Which shell are you using and exactly how did you set JAVA_HOME? Grails should respect your JAVA_HOME setting as long as it is visible to the grails command, for example in bash you must export the variable rather than just setting it, in tcsh you would use setenv rather than set.

$ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home

You can also remove /Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home/bin from your PATH as /usr/bin/java will automatically delegate to the appropriate java command for the current JAVA_HOME.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • The environment was it. I'm using the fish shell and I wasn't setting the right flag on the command to export the variable. For the record the right command is (use -x to export): set -x JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home – kennbrodhagen Sep 19 '13 at 13:13
  • 1
    It would be great if there was a way to do it local to grails as opposed to the whole system... – sparkyspider Oct 17 '13 at 09:43
  • 1
    @Spider I'd just create a shell script somewhere on your path (e.g. `~/bin/grails`) that sets JAVA_HOME and then delegates to the real grails script. Or for a single project do `grails wrapper` and edit the generated `grailsw`. – Ian Roberts Oct 17 '13 at 18:37
1

If you want to localise the Java version only to Grails the best way is to edit below file,

.gvm/bin/gvm-init.sh

You can set the JAVA_HOME in this file as below,

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home

I have Java 8 in my machine and here I am setting java 1.7 for grails ONLY.

  • Yep, this worked for me, even on Ubuntu, where I'd defined Java 8 as the default java using `alternatives`. My profile thinks I'm running Java 8 `javac -version` returns `javac 1.8.0_45`, but grails ignores /usr/bin/javac and instead searches under JAVA_HOME/bin for it. – ben3000 Jul 24 '15 at 05:33
1

For the new SDKman method you can export JAVA_HOME in [YOUR HOME]/.sdkman/candidates/grails/[concrete version or current]/bin/grails

Vasfed
  • 18,013
  • 10
  • 47
  • 53
racoonman
  • 11
  • 3
0

I needed to be able to switch between a Java 7/Grails 2.4.4 project and a Java 8/Spring 4 project in Ubuntu 12.04 and certain things made this difficult:

  • I'd set Java 8 as the default version after installing it using sudo apt-get install oracle-java8-set-default, but that apparently creates /etc/profile.d/jdk.sh and /etc/profile.d/jdk.csh containing JAVA_HOME, JRE_HOME and other env vars that prevented me from swapping the JDK.
  • This kinda worked, but the above env vars clouded things too much.

In the end, I removed both of the above items from my environment and the files in /etc/profile.d and I now:

  • Change the JDK by running sudo update-java-alternatives -s java-8-oracle (or java-7-oracle) as mentioned in the webupd8 article;
  • Run gvm to set the current or default grails and other tools as required

Seems messier than it should be, but I think it is working now.

ben3000
  • 4,629
  • 6
  • 24
  • 43
0

If you use SDKMAN to install Grails (which is the currently recommended method), you can add any versions of Java you have installed to SDKMAN and it will manage them for you as well. For example:

sdk install java openjdk-8 /usr/lib/jvm/java-8-openjdk-amd64
sdk use java openjdk-8

Note that this will set JAVA_HOME for your user, so if you don't want that you may want to consider one of the other options.

$ echo $JAVA_HOME
/home/user/.sdkman/candidates/java/current

For more information: SDKMAN local versions

bmaupin
  • 14,427
  • 5
  • 89
  • 94