Not able to switch java home properly in mac


Not able to switch java home properly in mac



I have two versions of Java in my mac and I am trying to switch from 1.8 to 1.7 for compiling one of my project which has 1.7 specific contents. Though I ran the following commands in the terminal:


$ alias setJdk1.7='export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)'
$ setJdk1.7



With that, I can see the proper version in java -version and mvn -version.


$ java -version

java version "1.7.0_181"
Java(TM) SE Runtime Environment (build 1.7.0_181-b09)
Java HotSpot(TM) 64-Bit Server VM (build 24.181-b09, mixed mode)

$ mvn -version

Apache Maven 3.0.5 (...)
Maven home: *mvn path*
Java version: 1.7.0_181, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_181.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.13.5", arch: "x86_64", family: "mac"



However, the /usr/libexec/java_home still seems to be pointing to the 1.8 java only.


$ echo $(/usr/libexec/java_home)
/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home



I am very new to mac and not really sure how I can toggle between versions. This is not allowing to successfully compile my project.




1 Answer
1



Maybe try using a function instead and setting up your command as follows:


setJdk1.7() {
export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
export PATH=$JAVA_HOME/bin:$PATH
}



Your command likely doesn't work because $JAVA_HOME is never added to your $PATH.


$JAVA_HOME


$PATH





This may be a dumb question, how do I execute a function? Should it be in a .bash? Anyways, I tried to run these two commands separately, the echo $JAVA_HOME gives the 1.7 version however the echo of /usr/libexec/java_home still shows 1.8
– SRS
Jul 2 at 3:11





To execute a function it's much like an alias, so you'd just do setJdk1.7 from the Terminal prompt. Can you please post the output of the command /usr/libexec/java_home -V.
– l'L'l
Jul 2 at 3:16


setJdk1.7


/usr/libexec/java_home -V





$ /usr/libexec/java_home -V Matching Java Virtual Machines (2): 1.8.0_151, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home 1.7.0_181, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_181.jdk/Contents/Home
– SRS
Jul 2 at 7:27





@SRS: Things seem in the correct place; are you always in the same terminal window while changing/checking versions? Also does export JAVA_HOME=$(/usr/libexec/java_home -v 1.7.0) make any difference (notice the zero on the end)?
– l'L'l
Jul 3 at 0:43


export JAVA_HOME=$(/usr/libexec/java_home -v 1.7.0)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages