Trouble Setting Up Maven on Mac OS - macos

I'm never used a mac before and this is the first time I've tried to setup a coding environment. I've gone through multiple of the pages on here that give suggestions as to what is wrong, but still getting mvn command not found
I've set my ~/.bash_profile as below:
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home
export JAVA_HOME
M2_HOME=/Applications/apache-3.6.1
export M2_HOME
PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin
export PATH
A java -version returns the following
java version "12.0.2" 2019-07-16
Java(TM) SE Runtime Environment (build 12.0.2+10)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing)
So I believe that to be fine
A echo $M2_HOME returns the following
/Applications/apache-3.6.1
Which from what I've seen would suggest that is also correct.
However, mvn -v returns the following:
-bash: mvn: command not found
I've tried restarting the terminal and source ~/.bash_profile
Any help greatly appreciated
James

A ~/.bash_profile like this works for me:
export JAVA_HOME=`/usr/libexec/java_home -v 12`
export M2_HOME=/Applications/apache-maven-3.6.1
export PATH=$PATH:$M2_HOME/bin
Make sure you have a valid Maven installation under /Applications/apache-maven-3.6.1 which has a bin folder inside with the actual executable.
Hope it helps.
Edit-1
Check the difference between:
/Applications/apache-3.6.1 vs.
/Applications/apache-maven-3.6.1

Related

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.

mvn command not found with environment variables set

Currently I am experiencing a problem with maven on my Mac OSx 10.10.5 Yosemite.
I installed the apache files today, and tried to set maven up only for it to not recognize itself.
I currently have a ~.bash_profile file with the three lines:
export JAVA_HOME=$(/usr/libexec/java_home)
export M2_HOME=/Users/vincenttomie/apache-maven-3.6.1/
export PATH=$PATH:$JAVA_HOME/bin:/Users/vincenttomie/apache-maven-
3.6.1/bin
When I do $Java_Home I get
/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home
When I do $M2_HOME I get
/Users/vincenttomie/apache-maven-3.6.1/
When I do echo $PATH I get below
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/vincenttomie/apache-maven-3.6.1/bin:/Users/vincenttomie/apache-maven-3.6.1/bin:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/bin:/Users/vincenttomie/apache-maven-3.6.1/bin
When I try to do mvn -- version in the terminal it gives me the
~bash: mvn: command not found.
I was wondering if anyone knew what the problem could be.
Thank you!

JAVA_HOME is not working in maven

java is installed at this path
$ which java
/usr/bin/java
mvn -version is giving this error
$ mvn -version
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
I have tried some of the solutions that were available online, but those don't work for me. Some of those solutions suggested adding
$export JAVA_HOME = /usr/libexec/java_home
or
$export JAVA_HOME = $(/usr/libexec/java_home)
to below files
~/.bashrc
~/.bash_profile
~/.profile
Also when I try to execute one shell command, it shows me error like
Error: JAVA_HOME is not defined correctly.
CARBON cannot execute /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin/java
After struggling for almost a day, I found out that maven is not reading the $JAVA_HOME from either of
~/.bashrc
~/.bash_profile
~/.profile
but it reads $JAVA_HOME from ~/.mavenrc
So finally, when I added
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home
in ~/.mavenrc then got output
mvn -v
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=1024m; support was removed in 8.0
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T15:39:06-04:00)
Maven home: /usr/local/Cellar/maven/3.5.0/libexec
Java version: 1.8.0_141, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre
Default locale: en_CA, platform encoding: UTF-8
OS name: "mac os x", version: "10.12.6", arch: "x86_64", family: "mac"
To fixed it, update the JAVA_HOME like following :
$ vim .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
$ source .bash_profile
Run Maven again :
$ mvn -version
maven reads JAVA_HOME from ~/.mavenrc. Add path for JAVA_HOME to ~./mavenrc and source it.
$ vim ~/.mavenrc
export JAVA_HOME=$(/usr/libexec/java_home)
$ source .mavenrc
On Ubuntu
I faced a similar problem. I configured $JAVA_HOME in /etc/environment
like JAVA_HOME=PATH_TO_JDK for example JAVA_HOME=/home/max/jdk1.8.0_144
Careful with
White space after path declaration JAVA_HOME=/home/max/jdk1.8.0_144[[_NO_WHITE_SPACE_AFTER_DECLARATION]]
Don't put any quotes, e.g. JAVA_HOME="/home/max/jdk1.8.0_144"
Don't put /bin, e.g. JAVA_HOME=/home/max/jdk1.8.0_144/bin <- This is wrong
Please check once there should be no ';' in JAVA_HOME value, if there is semicolon then it would show above error.
I had
JAVA_HOME
set but did not export it and maven was not picking up the correct JAVA_HOME.
Once I exported JAVA_HOME then it got resolved. Do not forget to export JAVA_HOME so that the variable is available to child processes (UNIX* like OS)
export JAVA_HOME=/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home

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.

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

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

Resources