Setting JAVA_HOME in zshrc on M1 Mac? - java

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

Related

How can Homebrew automatically update JAVA_HOME after it updates JDKs?

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.

Unsupported Java Version: Cannot start under Java 1.7: Java 1.8 or later is required

I just updated to IntelliJ IDEA 16 which comes with Java JDK 1.8. Fyi, I had downloaded Java JDK 1.8 a long time ago.
I tried to run my plugin that I am developing, and I cannot even do that, I get the following error message (slightly condensed)
"C:\Program Files\Java\jdk1.7.0_45\bin\java" -Xmx512m -Xms256m -XX:MaxPermSize=250m -ea "-Xbootclasspath/a:C:/Program Files (x86)/JetBrains/IntelliJ IDEA Community Edition 144.3143.6\lib\boot.jar" -Didea.config.path=C:\Users\Christopher.IdeaIC14\system\plugins-sandbox\config - ... -Dfile.encoding=windows-1252 -classpath "C:\Program Files\Java\jdk1.7.0_45\lib\tools.jar; ... ;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 144.3143.6\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.intellij.idea.Main
Unsupported Java Version: Cannot start under Java 1.7.0_45-b18: Java 1.8 or later is required.
So, I clearly know what the problem is; however, I cannot seem to figure out where the location to fix this is.
I have tried a lot of things so far; most involve using the search box in Settings and switching whatever I could to version 1.8. Also, I had updated my Environment Variable for Java from 1.6 to 1.8
With all of these changes, nothing is working! So how do I go about fixing this seemingly simple issue?
Set IDEA_JDK (or IDEA_JDK_64) environment variable.
Check Selecting the JDK version the IDE will run under
TL;DR --> Set JAVA_HOME to C:\Program Files\Java\jdk1.8.0_51
Java 8 is required to run IntelliJ IDEA starting from version 16 on all the supported platforms.
The actual JDK version used by the IDE can be verified in Help | About dialog (open any project to access the menu).
idea64.exe uses this JDK search in the following sequence:
IDEA_JDK_64 environment variable
..\jre64 directory
system Registry
JDK_HOME environment variable
JAVA_HOME environment variable
Under Arch Linux you can simply set java 8 as default (you must have it installed):
$ sudo archlinux-java set java-8-openjdk
In case anyone arrives here and hasn't had any luck with the other solutions, try this. I'm using PhpStorm under OSX, but the steps are probably applicable to JetBrains' other IDEs too:
Navigate to your IDE's config folder (see this article to learn how to find this folder's location).
Open the idea.propeties file.
If a JVMVersion attribute exists, delete that entire entry. If it doesn't exist, then this probably won't be of much help!
Save the file.
Launch your IDE.
Just set JAVA_HOME system variable to your JDK 8:
Run in cmd
setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0_111"
don't foget to change to your java path
In my case, Ubuntu 14 (32-bit), I opened the file:
/home/<user>/Programs/PhpStorm/bin/phpstorm.sh
and after
if [ "$JDK" = "" ] && [ "$OS_TYPE" = "Linux" ] && ........... fi
I added:
if [ "$JDK" = "" ]; then
JDK="/usr/lib/jvm/java-8-oracle"
fi
And then it works!
Double check that your JAVA_HOME points to the correct 1.8 Installation and make sure that in the PATH env there is no reference to "C:\Program Files\Java\jdk1.7.0_45\bin".
Try to print your JAVA_HOME/PATH from the Console and see what the output is. If your changes are not reflected logout and then login again.
IntelliJ 16 REQUIRES Java 8 to run, it won't run under Java 7. It's compiled to Java 8 classes so it can't ever be run on an older JVM.
This is well and clearly mentioned in the documentation.
So if you have your system JVM be an older one, it will fail to start unless you coerce it into using a non-default JVM.
Possibly you have some java related executables left over in your Windows/System32 directory, where some older Java installers would drop them. Delete these by hand from the file system and see what happens.
Just in case this page needs one more recommendation, I fixed the problem in my case by going to the IDE's config folder (as #Nate puts it, see here for how to locate it), and deleting the idea.jdk file (which held a reference to a 1.6 VM).
Apart from setting the correct Java version for the global, per project and per module SDK, for the Java Compiler, and for the language level, you also must set the Target bytecode version both for the project and (if you use maven) for each maven module.
Often the project bytecode version is correct but the module bytecode version is lower, i.e. 1.5.
The per-module bytecode version is not displayed when opening the default preferences via File > Other Settings > Default Settings. You must edit the current preferences either via a shortcut (i.e. ⌘+,) or from the main menu as shown below (for mac):
I had the same problem. All paths pointing to java jdk 8, but still throwing the error. I was able to run Android Studio by running the "studio64.exe" instead "studio.exe" in the bin folder

Unix Java path variables is being reset

I have a machine running RHEL 6. Currently JDK 7 is installed. For ElasticSearch I need to move to JDK 8. SO i copied the JDK 1.8 to /opt/. THen ran the below steps.
update-alternatives --install /usr/bin/java java /opt/jdk1.8.0_45/bin/java 100
update-alternatives --config java
Did the same thing for Javac as well.
Then did the below changes to Environment variables
export JAVA_HOME=/opt/jdk1.8.0_45/
export PATH=/opt/jdk1.8.0_45/bin:...
In setting path, i copied the existing path, removed the jdk7 entries and added the entire path again.
After this when i ran java -version i got 8. But on restarting the shell, it reverted back to 1.7!!!
So i checked internet and found that i need to update the /etc/profile file
So added below two entries..
export JAVA_HOME=/opt/jdk1.8.0_66/
export PATH=/opt/jdk1.8.0_66/bin:$PATH
But even afer this, the JDK 1.7 is being append to teh Path variable.
Any pointers on how i can move to JDK 1.8
I got it working. There was a script file inside /etc/profiles.d/. It was named as jdk.sh. There the JAVA_HOME and PATH was being set. I changed this to the jdk1.8 and the issue was solved. Thanks. i got a comment from Stackoverflow about the way to set the Path using script inside profile.d folder. So searched there and found the script. Took me 2 hours to figure this out... – Shabin Hashim

JAVA_HOME and java -version

I'm using a Windows .bat script and I set JAVA_HOME as C:/Program Files/Java/jdk1.6.0_32
when I do a java -version, it still shows the 1.3
How can I fix this? What am I doing wrong?
For me the issue was in my PATH variable, C:\ProgramData\Oracle\Java\javapath; was added by java windows install before my %JAVA_HOME%\bin;.
So I'd echo %JAVA_HOME% pointing to a JDK7 and java -version showing jdk8.
I'd to put %JAVA_HOME%\bin; before C:\ProgramData\Oracle\Java\javapath; so that java -version displays jdk7.
Try %JAVA_HOME%\bin\java -version
If you modify JAVA_HOME, it's usually better to invoke java with an absolute path (using JAVA_HOME) because the new binary is probably not in the path (and then Windows will load the wrong binary).
Make sure that the PATH environment variable is pointing to %JAVA_HOME%\bin.
Be sure not to mix the system variable path and the user variable system path. I feel OK in calling java without the absolute path (when I know how JAVA_HOME and PATH are configured).
Calling java -version from command line, causes cmd.exe to do the lookup on the "known" directories. "Known" means PATH environment variable. It seems that your PATH contains a java 1.3 bin folder, and not 1.6.
JAVA_HOME is another variable, that is used (for example, and not only) by java wrappers, or by scripts executing some java stuff.
Try doing this:
SET JAVA_HOME=C:/Program Files/Java/jdk1.6.0_32
%JAVA_HOME%/bin/java -version
Add quotes where needed.
I had similar issue,in my case , I had two versions java installed. it can be fixed by uninstalling one version of java completely from system.
Had a similar scenario today - two Windows 10 devices - both have JRE 1.6 & 1.7.
When typing
Java -version
One device shows 1.6 the other 1.7.
This was preventing me running a third party JAR to install some software on the device showing 1.6 (which worked fine on the device showing 1.7 when running java -version), using:
java -jar ThirdParty.jar
As the JAR needed to be run by 1.7.
Cause of this was in the PATH environment variable - one device had the location of 1.6 first in the PATH list, moving the 1.7 location above the 1.6 location resulted in consistency using Java -version and allowed me to install the software.
java -version will consult the paths in the special environment variable Path. You need to select the java version you want and move it upwards the latter (click "Move Up"). You probably have that reference to JDK 1.3 in Path above your addition of JDK 1.6. Since that's the first thing the OS finds, that's what it chooses to run.
Executing the command again with the same window opened after changing the environment variables will not work. Re-open it
I know this question is old but this was my case and I wanted to re-explain further, similar to #DanBot 's case

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