How can Homebrew automatically update JAVA_HOME after it updates JDKs? - java

I'm using macOS, when homebrew updates the JDK I have to manually update the $JAVA_HOME path in .zshrc since it uses the version number in its path, just replace the version number to a newer one like
/usr/local/Cellar/openjdk#11/11.0.14/libexec/openjdk.jdk/Contents/Home
to
/usr/local/Cellar/openjdk#11/11.0.16/libexec/openjdk.jdk/Contents/Home
As you can see there is just a difference in version numbers, other directory names are still the same. Is there any way to automatically update JAVA_HOME to the path that Homebrew just updated?

You can use the default macOS command java_home:
% /usr/libexec/java_home -v 11
/opt/homebrew/Cellar/openjdk#11/11.0.16.1/libexec/openjdk.jdk/Contents/Home
And put this in your .zshrc:
export JAVA_HOME=`/usr/libexec/java_home -v 11`
From man java_home:
java_home - return a value for $JAVA_HOME

I eventually figured out what the problem is. I found a difference between openjdk#11 and adoptopenjdk11 installed with Homebrew.
The situation was I actually got 3 java paths on my mac, jre8, openjdk#11 and openjdk#8.
In my opinion, openjdk#{xx} is like unregistered binaries that are not bound with java_home (brew formulae), however, adoptopenjdk{xx} are more like registered ones(brew cask).
And what made this situation more complex is the jre8 downloaded from Download Java for macOS which pinned the java_home to
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
That is why I could not find any other JDK paths (other than the one I downloaded from the java official website) through executing
$ /usr/libexec/java_home -V
since it actually searches for and lists Java Virtual Machines which are included by JREs.
This relates to a common confusion that new developers sometimes would have - the difference between JDK JRE and even JVM (What is the difference between JDK and JRE?).
So the solution is installing adoptopenjdk{xx} if you are not strictly sticking with openjdk#{xx}, it would register the path of its JRE-contained JVM to the variable java_home , and it's ready to go with export in the bash/zshell profile.
Thanks to #Ortomala Lokni and #g00se as they provide useful info that inspired me to look deeper into this.

Related

Setting JAVA_HOME in zshrc on M1 Mac?

When I use
/usr/libexec/java_home
I get
/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home
but I want to be getting
/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home
I've changed my .zshrc file a number of times and it seems to always revert to the default of temurin 17.
My application will not run android pointing to temurin 17 and it should be selecting zulu 11 jdk.
One thing you should do after changing .zshrc file is to execute:
source ~/.zshrc in order for the changes in your .zshrc to be activated
But I would suggest when working with multiple java version, to setup alias and be able to easily change versions on-demand.
You can find a guide for this on this Stackoverflow answer:
https://stackoverflow.com/a/40754792/2352196

How to set Java version for SBT

I am trying to run a scala program, in which there are errors with Java 16. My colleague is using Java 15, and all is fine. When i type java -version in my terminal it says i am using Java 15. However, when i run sbt run -v, it says it is using Java 16, and thus the program throws errors.
I am seeing people talk about this sbt-extra thing, but not a whole lot of explanation on how to use it. I do not even have Java 16 installed on my Mac, so I am really confused as to why SBT says this.
I think I have faced a similar issue. It happened because you did not set the Java_Home part. If you are using a mac, you have to set Java_Home path in .bashrc or .zshrc file which one you are using. I think it should work.
To handle your installed jvms you can use Jenv.
To install jenv:
git clone https://github.com/jenv/jenv.git ~/.jenv
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(jenv init -)"' >> ~/.bash_profile
Then, you can add your intalled jvms. In Mac, if you have installed them via brew you can find those in: /Library/Java/JavaVirtualMachines.
Then add them to jenv:
jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
You can see the available jvms in jenv:
you can set the default jvm with the command:
jenv global 1.8.0.121
Then, execute sbt in some of your projects and you should see that jvm as the jvm that sbt is using.
Another option which worked for me is to add the version of java you want to be used to the front of your terminal PATH environment variable. Since I used homebrew to install openjdk, I used the path they suggested resulting in the following path to use openjdk version 11.
export PATH="usr/local/opt/openjdk#11/bin:$PATH"
Note - the openjdk path I used I think is just the homebrew symlink to the actual java installation which is in /Library/Java/JavaVirtualMachines. You could probably just use that actual path but I didn't test it.

Choose updated Java SDK in mac from available different version

I'm using mac machine for native-script development and while executing an program it thrown an java error that:
Javac version 1.6.0_65 is not supported. You have to install at least 1.8.0.
so I checked with available install version on developer machine & found two different version detail:
/usr/bin/java -version Showing 1.6.
while system preference -> java control panel -> update. showing V1.8
any suggestion why two version !! Am I missing something here?
Update1: Following help to understand how mac handling this: /usr/bin/java is machine default location, and /Library/Internet.. which is manage explicit.
sudo rm /usr/bin/java
sudo ln -s /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java /usr/bin
In my case I update default one with downloaded from internet.
Reference link Link1, Link2
You should use /usr/libexec/java_home instead
> /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
you can use it to set JAVA_HOME
export JAVA_HOME=$(/usr/libexec/java_home)
then, you can put this one inside ~/.profile so you have always JAVA_HOME set to most recent release.

How do I install an earlier version of Java SDK on OSX

I have the Java 1.8.0_45 SDK installed on OSX Yosemite (10.10.4), but because of a bug in this release I need to go back to 1.8.0_25
I have downloaded and installed the earlier version (1.8.0_25) but even after a reboot java -versionstill shows 1.8.0_45.
I don't really understand where Java resides on OSX, but how can I get my system back so it uses 1.8.0_25
Try and add this to your ~/.bashrc
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home
You can have multiple JRE/JDK's installed, by changing this path, you can specify which one you use each time you open a new shell.
Here is what I use in my .bashrc
JAVA_VERSION=7
JAVA_7_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home
JAVA_8_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home
tmp="JAVA_${JAVA_VERSION}_HOME"
export JAVA_HOME=${!tmp}
export PATH=${!j}/bin:$PATH
Here, you can simply change the 7 to an 8.
This will change the JAVA_HOME, and append the bin directory to your path for general use from the command line.
note you may beed to change your java home's according to the specific release versions installed on your machine.
Ah found it, suprisingly easy:
macbook:JavaVirtualMachines paul$ cd /Library/Java/JavaVirtualMachines
macbook:JavaVirtualMachines paul$ ls
jdk1.7.0_40.jdk jdk1.7.0_45.jdk jdk1.8.0.jdk jdk1.8.0_05.jdk jdk1.8.0_20.jdk jdk1.8.0_25.jdk jdk1.8.0_45.jdk
macbook:JavaVirtualMachines paul$ sudo rm -fr jdk1.8.0_45

Running Ant with JDK 1.6 on Mac OS X

I am having a problem running Ant with JDK 1.6 on Mac OS X. Even though Java application versions is set to Java SE 6 in OS X's Java Preference, executing java -version in Terminal also shows java version "1.6.0_07", Ant still seems to use JDK 1.5 to be using JDK 1.5 as it does not see JDK 1.6 classes when compiling my code.
I understand that Ant relies on JAVA_HOME environment variable to specify which JDK to use. However, I do not quite understand how this variable can be set on Mac OS X.
Hence, my question is how to make Ant runs with JDK 1.6 on Mac OS X. If the correct way is still to set JAVA_HOME environment variable, how to set the variable on OS X.
The JAVA_HOME environment variable is set in your home directory's .profile file. (/Users/ejel/.profile ?) Edit it and set it to what you want it to be. E.g.:
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
From this point onward, every time you open a new terminal window it will have JAVA_HOME set to this new value. It will not have changed with any existing open windows.
If you are truly aghast to putting this in the profile, or if it conflicts with other software, the export statement could always be run in the terminal manually or go into a script (eg: setj6ev.sh) that is run once before you start running ant tasks.
I've added the line
export JAVA_HOME=`/usr/libexec/java_home`
To my .zshrc file, it seems to do the trick (.bash_profile or whatever if you use bash).
Ted, using the Java Preferences app doesn't change the CurrentJDK symlink in /System/Library/Frameworks/JavaVM.framework/Versions, which is what Ant will use if the JAVA_HOME environment variable isn't set. Thus, you can either change that symlink manually or set the JAVA_HOME environment variable, but if you do neither, then Ant won't use the correct JDK.
You can see the version of the jdk that Ant is using by issuing an <echo message="${ant.java.version}"/> in your build.xml file.
Explicitly setting the JAVA_HOME variable in your .profile/.bashrc/.zshrc isn't actually the recommended way to do it on the mac. There are programs that I've seen get hosed up with an explicitly set JAVA_HOME to a particular version (grails 1.1 with some spring resources for example).
The correct way to set the version of Java that you want to use is to use the /Application/Utilities/Java Preferences.app application.
In there, you drag the version of java that you want to use to the top. This will enable that version for all applications (both those run from the command line and those launched through GUI processes).
You can test the current version by running this from the command line:
java -version
I don't actually like the way that the mac handles the entire set of java symlinked directories and files. It's not obvious and people often screw it up.
See the apple developer page on this for more details.
I try everything, and only one thing works for me : unlink CurrentJDK, and link to 1.6 :
cd /System/Library/Frameworks/JavaVM.framework/Versions
sudo unlink CurrentJDK
sudo ln -sF "1.6" CurrentJDK
Finally I get :
java -version
java version "1.6.0_22"
I hope this help.
You may need to open a new command prompt instance so that the shell can pick up any changes to the environment variables.

Categories

Resources