How do I check if the Java JDK is installed on Mac? - macos

How do you check if Java SDK is installed on a Mac?
Is there a command line for this?

javac -version in a terminal will do

You can leverage the java_home helper binary on OS X for what you're looking for.
To list all versions of installed JDK:
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
1.8.0_51, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home
1.7.0_79, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home
To request the JAVA_HOME path of a specific JDK version, you can do:
$ /usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home
$ /usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home
You could take advantage of the above commands in your script like this:
REQUESTED_JAVA_VERSION="1.7"
if POSSIBLE_JAVA_HOME="$(/usr/libexec/java_home -v $REQUESTED_JAVA_VERSION 2>/dev/null)"; then
# Do this if you want to export JAVA_HOME
export JAVA_HOME="$POSSIBLE_JAVA_HOME"
echo "Java SDK is installed"
else
echo "Did not find any installed JDK for version $REQUESTED_JAVA_VERSION"
fi
You might be able to do if-else and check for multiple different versions of java as well.
If you prefer XML output, java_home also has a -X option to output in XML.
$ /usr/libexec/java_home --help
Usage: java_home [options...]
Returns the path to a Java home directory from the current user's settings.
Options:
[-v/--version <version>] Filter Java versions in the "JVMVersion" form 1.X(+ or *).
[-a/--arch <architecture>] Filter JVMs matching architecture (i386, x86_64, etc).
[-d/--datamodel <datamodel>] Filter JVMs capable of -d32 or -d64
[-t/--task <task>] Use the JVM list for a specific task (Applets, WebStart, BundledApp, JNI, or CommandLine)
[-F/--failfast] Fail when filters return no JVMs, do not continue with default.
[ --exec <command> ...] Execute the $JAVA_HOME/bin/<command> with the remaining arguments.
[-R/--request] Request installation of a Java Runtime if not installed.
[-X/--xml] Print full JVM list and additional data as XML plist.
[-V/--verbose] Print full JVM list with architectures.
[-h/--help] This usage information.

Type in a terminal:
which javac
It should show you something like
/usr/bin/javac

Below command worked out pretty good:
javac -version
I also manually verified by navigating to the Java Folder on my Mac
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk

/usr/bin/java_home tool returns 1 if java not installed.
So you can check if java is installed by the next way:
/usr/libexec/java_home &> /dev/null && echo "installed" || echo "not installed"

Open terminal.
run command to see:
javac -version
Also you can verify manually by going to the specific location and then check. To do this run below command in the mac terminal
cd /Library/Java/JavaVirtualMachines/
Then run ls command in the terminal again. Now you can see the jdk version & package if exists in your computer.

If you are on Mac OS Big Sur, then you probably have a messed up java installation.
I found info on how to fix the issue with this article:
https://knasmueller.net/how-to-install-java-openjdk-15-on-macos-big-sur
Download the .tar.gz file of the JDK on https://jdk.java.net/15/
Navigate to the download folder, and run these commands (move the .tar.gz file, extract it and remove it after extraction):
sudo mv openjdk-15.0.2_osx-x64_bin.tar.gz /Library/Java/JavaVirtualMachines/
cd /Library/Java/JavaVirtualMachines/
sudo tar -xzf openjdk-15.0.2_osx-x64_bin.tar.gz
sudo rm openjdk-15.0.2_osx-x64_bin.tar.gz
Note: it might be 15.0.3 or higher, depending on the date of your download.
run /usr/libexec/java_home -v15 and copy the output
add this line to your .bash_profile or .zshrc file, depending on which shell you are using. You will probably have only one of these files existing in your home directory (~/.bash_profile or ~/.zshrc).
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home
save the changes and make them effective right away by running:
source ~/.bash_profile or source ~/.zshrc
check that java is working - run java -v

Just type javac. If it is installed you get usage information, otherwise it would just ask if you would like to install Java.

Make sure you correctly define the project's JDK and restart IntelliJ (full restart).

On MAC find your JDK path by executing the command.
/usr/libexec/java_home

Related

How to install Maven in Mac Catalina?

I just installed Maven via Mac Catalina via command line and mvn -version was showing output. then it went away. Please help.
May be a long post, but in the end you will have a convenient tool to install packages and understanding of how to configure them.
Why maven "disappeared" I have no idea of, so what I would do in this situation:
First of all, I would delete maven manually (find the directory and remove it along with any other files that were setup during maven installation)
Then I would highly recommend using Homebrew - a package manager for macOS
To install Homebrew, open terminal and execute the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Wait for a couple of minutes (a less) until it is installed
Now, that Homebrew is installed, you have a very convenient tool to install stuff on your mac
So, you want to install Maven. You can find Maven on Homebrew
Now you just have to open your terminal again and execute
brew install maven
After a few seconds brew will display the directory it installed Maven in (by default, all Homebrew packages are installed in /usr/local/Cellar/)
You are almost there, now you need to set the environment variable.
As described by Apache, the environment variable needs to be added to the PATH environment variable.
Here I have some uncertainty in terms of what file to use: .bash_profile or zprofile. The confusion is caused by the fact that in latest macOS update (maybe several latest updates) Apple decided to use zsh shell, and so .bash_profile became somewhat functionally equivalent to zprofile. There are many articles on the Internet about what file to use, so you better check them out before proceeding (sorry for not providing links).
On my Catalina 10.15.6 I use .zprofile and everything looks to work perfectly.
Once you decide what file to use, execute the following in the terminal:
nano .zprofile
An editor will open, write this:
export PATH=/usr/local/Cellar/maven/*version*/bin:$PATH
Press Control + X to exit, then Y to save changes and hit Enter to exit the editor
After that you will again be in terminal, where execute:
source .zprofile
To load it. Then close the terminal, open again and check that PATH variable is edited by executing:
env
That's it! Good luck coding!
For MacOs Catalina, below is what worked for me.
Terminal > Vim .zprofile
Add:
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
export JAVA_HOME
M2_HOME=/Users/username/apache-maven-3.8.1 (This is where my maven folder is.)
export M2_HOME
PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin
export PATH
Terminal > source ~/.bash_profile
Restart Terminal > mvn -version
Output:
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: /Users/username/apache-maven-3.8.1
Java version: 1.8.0_281, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre
Default locale: en_IN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.7", arch: "x86_64", family: "mac"
username#C02F ~ % java -version
java version "1.8.0_281"
Java(TM) SE Runtime Environment (build 1.8.0_281-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.281-b09, mixed mode)
Did you try executing:
source ~/.zshrc

macbook 10.15.5 install maven failed: NB: JAVA_HOME should point to a JDK not a JRE

I know there are a lot of questions/answers about this question, but all were failed in my case.
I installed jdk 1.8 and then installed maven3.6.3, java runs well but mvn command didn't work, see my command output and env:
java -version
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
mvn -v
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
mac OS version: 10.15.5
echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home
echo $PATH
/usr/local/apache-maven-3.6.3/bin:/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:.
config in ~/.bash_profile
export M2_HOME=/usr/local/apache-maven-3.6.3
PATH=$M2_HOME/bin:$JAVA_HOME/bin:$PATH:.
#Mac OSX 10.15.5 or later version need this configure
export JAVA_HOME=$(/usr/libexec/java_home)
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
export PATH
I also source ~./bash_profile to make configure effective.
I would suggest revert all the changes you have made in ~/.bash_profile. Checkout my way to make the maven work:
Download latest maven zip package apache-maven-<version>-bin.zip from Apache maven download page.
Unzip it to any place, e.g: ~/Develop/apache-maven-3.6.0
Create a link with command sudo ln -sf ~/Develop/apache-maven-3.6.0/bin/mvn /usr/local/bin/mvn (change version number per your case, if the local bin folder missing, just create it mkdir -p /usr/local/bin).
Check the installation mvn --version
Hope it works for you.
After hundreds updating configure file, now mvn -version can works, share my update and hope it can help you.
fill or update vim ~/.mavenrc(in the beginning, this file isn't exist)
# my MacOS is 10.15+
export Java_HOME=$(/usr/libexec/Java_home)
and then source ~/.mavenrc to let system env effective.
Summary:for my mac system, seems maven couldn't get JAVA_HOME from .bash_profile or .profile, so if you confirm your JAVA_HOME is set correctly and MAVEN_HOME configured, then you can have a try in this way.

Unable to find any JVMs matching version "1.8.0_40" when open terminal on macOS

I have installed jdk1.8.0_121 already and JAVA_HOME seems correct.
But when I open terminal, it always reminds me "Unable to find any JVMs matching version "1.8.0_40"".
jdk1.8.0_40 was installed Previously but I hava already uninstalled this version.
Does anyone know how to remove this annoying reminder?
Thanks a lot.
MacOS X does not come with Java Development Kits (JDKs) pre-installed.
Run java -version in the terminal and you may be prompted to install Java. Unless you already have JVM installed, in which case you will see the build version number.
Next, you will be prompted to visit the Java SE Development Kit Downloads page. Download one of the JDK (Java Development Kits) with a file extension ending in <filename>.tar.gz.
Then run tar -xf <filename>.tar.gz.
Rename the newly created directory to JAVA_HOME.
i solved this using this command in terminal
brew install --cask homebrew/cask-versions/adoptopenjdk8
#sideshowbarker, you're right. I checked all shell initialization files and found the java version was set as 1.8.0_40 in /etc/profile.
Thanks again.
please use like brew cask install adoptopenjdk8 to install deprecated java version, esp. for Android Studio.
use java_home -V to show all install version of java, like:
/usr/libexec/java_home -V
Matching Java Virtual Machines (5):
12.0.2, x86_64: "Java SE 12.0.2" /Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home
12.0.1, x86_64: "OpenJDK 12.0.1" /Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk/Contents/Home
1.8.0_222, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
1.6.0_65-b14-468, x86_64: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-468, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
and within .zshrc or .bash_profile for setting default jdk, like use command setjdk 1.8
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
function setjdk() {
export JAVA_HOME=`/usr/libexec/java_home -v $#`
}
and with recent java/jdk v12, the Android Studio CLI not work:
$setjdk 12
$avdmanager
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
at com.android.sdklib.tool.AvdManagerCli.run(AvdManagerCli.java:213)
at com.android.sdklib.tool.AvdManagerCli.main(AvdManagerCli.java:200)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 5 more
I have faced the JVM issue while installing flutter, so my finding is particularly based on macOS 10.15.5, if you install the latest java the JVM error will still show up. For that, you have to install legacy version of java from apple.
Download the java from here
https://support.apple.com/kb/DL1572?locale=en_GB
then
Launchpad -> Other -> Script Editor
create a new document and paste the below code as it is
set theDMG to choose file with prompt "Please select javaforosx.dmg:" of type {"dmg"}
do shell script "hdiutil mount " & quoted form of POSIX path of theDMG
do shell script "pkgutil --expand /Volumes/Java\ for\ macOS\ 2017-001/JavaForOSX.pkg ~/tmp"
do shell script "hdiutil unmount /Volumes/Java\ for\ macOS\ 2017-001/"
do shell script "sed -i '' 's/return false/return true/g' ~/tmp/Distribution"
do shell script "pkgutil --flatten ~/tmp ~/Desktop/ModifiedJava6Install.pkg"
do shell script "rm -rf ~/tmp"
display dialog "Modified ModifiedJava6Install.pkg saved on desktop" buttons {"Ok"}
then Script -> Compile and then Script -> Run.
a popup will ask you to locate your downloaded file javaforosx.pkg
Running the script will create a ModifiedJava6Install.pkg on your desktop.
Run this ModifiedJava6Install.pkg to install the legacy Java version.

Having trouble getting java to work on Mac terminal

I am having trouble getting java to work. I type java -version into my terminal and get java: no such file or directory. My Mac OS X version is 10.11.1. I have the newest JDK installed 1.8.0_73 x64. I am trying to compile java files and eventually create a jar file. Tutorials just say to type in java -version. Is there something I am missing in order to use the JDK? Taylors-MacBook-Pro-4:~ Taylor$ java -version
-bash: java: No such file or directory
EDIT: Echoing the $PATH gives me the following.
Taylors-MacBook-Pro-4:~ Taylor$ echo $PATH
Taylors-MacBook-Pro-4:~ Taylor$
It might be a JAVA_HOME issue. Type:
$ export JAVA_HOME=<correct path to your java virtual machine home folder>
For me it's something like:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
It looks like your $PATH is not set properly if indeed java is installed.
Type $ echo $PATH and see the list. Usually java is installed at /usr/bin/ or /usr/local/bin/ directory.

Cmake -version always picking up older version of the software

I am trying to install Gflags which inturn requires Cmake 2.8.12 or above. I currently have Cmake 2.8.11 and i tried to install the latest version. Installation went thru without any problem, but when I run cmake -version, i still see the older version. I have tried rebooting my machine.
Hardware : MAC 10.9
looks like a newer version of binary cmake placed in the $PATH after the old one or not in the $PATH at all. To check what verstion takes precedence try in bash command prompt:
$ which cmake
to see other versions:
$ whereis cmake
fix your PATH accordingly (in system-wide profile or your personal ~/.bashrc) and reload bash by exec bash or close and reopen terminal window.
Anyway, you always may execute cmake by full (absolute) path:
$ cd your-project-src
$ mkdir build
$ cd build
$ /full/path/to/cmake ..

Resources