How to set JAVA_HOME from the ubuntu shell on WSL2? - java

I installed WSL2 on my Windows 10 Home. I want to use Maven (which I verified is installed correctly on Windows).
In an ubuntu shell from Windows Terminal, I set JAVA_HOME in my .profile but it would not get set.
I ran these commands to try out the correct way to set JAVA_HOME but they do not work.
user#DESKTOP:/mnt/c/Users/user$ mvn
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
user#DESKTOP:/mnt/c/Users/user$ JAVA_HOME=/mnt/c/Program\ Files/Java/jdk-11.0.10/bin
user#DESKTOP:/mnt/c/Users/user$ cd $JAVA_HOME
-bash: cd: too many arguments
user#DESKTOP:/mnt/c/Users/user$ cd /mnt/c/Program\ Files/Java/jdk-11.0.10/bin
user#DESKTOP:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ 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
user#DESKTOP:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ JAVA_HOME=/mnt/c/'Program\ Files'/Java/jdk-11.0.10/bin
user#DESKTOP:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ cd $JAVA_HOME
-bash: cd: too many arguments
user#DESKTOP:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ JAVA_HOME=/mnt/c/'Program\ Files'/Java/jdk-11.0.10
user#DESKTOP:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ cd $JAVA_HOME
-bash: cd: too many arguments
user#DESKTOP:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ cd /mnt/c/Program\ Files/Java/jdk-11.0.10/bin
user#DESKTOP:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ pwd
/mnt/c/Program Files/Java/jdk-11.0.10/bin
user#DESKTOP:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ echo $JAVA_HOME
/mnt/c/Program\ Files/Java/jdk-11.0.10
user#DESKTOP:/mnt/c/Program Files/Java/jdk-11.0.10/bin$
user#DESKTOP:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ export JAVA_HOME="/mnt/c/Program Files/Java/jdk-11.0.10"
user#DESKTOP:/mnt/c/Program Files/Java/jdk-11.0.10/bin$ cd $JAVA_HOME
-bash: cd: too many arguments
user#DESKTOP:~$ export JAVA_HOME="/mnt/c/Program\ Files/Java/jdk-11.0.10"
user#DESKTOP:~$ cd $JAVA_HOME
-bash: cd: too many arguments
UPDATE:
I followed user Aran's (unfortunately deleted) answer below - I edited /etc/environment and added
export JAVA_HOME="/mnt/c/Program Files/Java/jdk-11.0.10"
then in .bashrc I added
source /etc/environment
and thought it worked because when I did an env in the ubuntu shell, I saw JAVA_HOME
I installed mvn and it still gives the same error:
$ mvn 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 think it is the space in "Program Files".
I do have JDK 11 installed. Verified by navigating to JAVA_HOME/bin and I can see javac.exe.
So the issue is not fixed.
UPDATE 2
Here is what the Maven script tries to do with JAVA_HOME, which may help find the correct workaround :
if [ -z "$JAVA_HOME" ] ; then
JAVACMD=`which java`
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
echo "The JAVA_HOME environment variable is not defined correctly" >&2
echo "This environment variable is needed to run this program" >&2
echo "NB: JAVA_HOME should point to a JDK not a JRE" >&2
exit 1
fi
Please note that the script goes through the "else" part, and that the "! -x" part is where it fails.
Note: This is not a duplicate of this question.
I do not have access to how Maven is accessing JAVA_HOME.
The question is not about cd $JAVA_HOME. It is about using mvn Maven. If Maven is using $JAVA_HOME internally, there is no way I can see how to make it use "$JAVA_HOME"

Add copy of java.exe with the name just "java" . That will prevent mvn from complaining - it looks for "$JAVACMD"which is defined as JAVACMD="$JAVA_HOME/bin/java"

Workaround: (not an exact solution to the question)
Set the JAVA_HOME variable from Windows, via the gui and use Windows Powershell instead of ubuntu shell.

I solved it by installing OpenJDK 17 on WSL(link below).
Run the following commands in WSL (I was using Windows Terminal)
sudo apt update
sudo apt install openjdk-17-jdk
java --version
Link: https://techviewleo.com/install-java-openjdk-on-ubuntu-linux/

Related

No JRE found. Please make sure $RIDER_JDK, $JDK_HOME, or $JAVA_HOME point to valid JRE installation [duplicate]

I just installed JDK in Ubuntu with sudo apt-get install openjdk-6-jdk command,
after the installation where's the Java bin directory located? And how can I set the environment path for that directory? I have little experience with Ubuntu, can anyone give some advice or suggest any good website for reference?
set environment variables as follows
Edit the system Path file /etc/profile
sudo gedit /etc/profile
Add following lines in end
JAVA_HOME=/usr/lib/jvm/jdk1.7.0
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH
Then Log out and Log in ubuntu for setting up the paths...
Java is typically installed in /usr/java
locate the version you have and then do the following:
Assuming you are using bash (if you are just starting off, i recommend bash over other shells) you can simply type in bash to start it.
Edit your ~/.bashrc file and add the paths as follows:
for eg. vi ~/.bashrc
insert following lines:
export JAVA_HOME=/usr/java/<your version of java>
export PATH=${PATH}:${JAVA_HOME}/bin
after you save the changes, exit and restart your bash or just type in bash to start a new shell
Type in export to ensure paths are right.
Type in java -version to ensure Java is accessible.
Ubuntu installs openjdk6 to /usr/lib/jvm/java-6-openjdk path. So you will have the bin in /usr/lib/jvm/java-6-openjdk/bin. Usually the classpath is automatically set for the java & related executables.
To Set JAVA_HOME / PATH for a single user, Login to your account and open .bash_profile file
$ vi ~/.bash_profile
Set JAVA_HOME as follows using syntax export JAVA_HOME=<path-to-java>. If your path is set to /usr/java/jdk1.5.0_07/bin/java, set it as follows:
export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
Set PATH as follows:
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
Feel free to replace /usr/java/jdk1.5.0_07 as per your setup. Save and close the file. Just logout and login back to see new changes. Alternatively, type the following command to activate the new path settings immediately:
$ source ~/.bash_profile
OR
$ . ~/.bash_profile
Verify new settings:
$ echo $JAVA_HOME
$ echo $PATH
Tip: Use the following command to find out exact path to which java executable under UNIX / Linux:
$ which java
Please note that the file ~/.bashrc is similar, with the exception that ~/.bash_profile runs only for Bash login shells and .bashrc runs for every new Bash shell.
To Set JAVA_HOME / PATH for all user, You need to setup global config in /etc/profile OR /etc/bash.bashrc file for all users:
# vi /etc/profile
Next setup PATH / JAVA_PATH variables as follows:
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
Save and close the file. Once again you need to type the following command to activate the path settings immediately:
# source /etc/profile
OR
# . /etc/profile
You need to set the $JAVA_HOME variable.
In my case while setting up Maven, I had to set it up to where JDK is installed.
First find out where JAVA is installed:
$ whereis java
java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz
Now dig deeper:
$ ls -l /usr/bin/java
lrwxrwxrwx 1 root root 46 Aug 25 2018 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Dig deeper:
$ ls -l /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
-rwxr-xr-x 1 root root 6464 Mar 14 18:28 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
As it is not being referenced to any other directory, we'll use this.
Open /etc/environment using nano:
$ sudo nano /etc/environment
Append the following lines
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
export JAVA_HOME
Reload PATH using:
$. /etc/environment
Now:
$ echo $JAVA_HOME
Here is your output:
/usr/lib/jvm/java-1.8.0-openjdk-amd64
Sources I referred to:
https://askubuntu.com/a/175519
https://stackoverflow.com/a/23427862/6297483
if you have intalled only openJDK, the you should update your links, because you can have some OpenJDK intallation.
sudo update-alternatives --config java
after this
$gedit ~/.bashrc
add the following line in the file
JAVA_HOME=/usr/lib/jvm/YOUR_JAVA_VERSION
export PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME
you can get you java version with
java -version
Open terminal (Ctrl+Alt+t)
Type
sudo gedit .bashrc
Enter password of ubuntu user
Go to last line of the file
Type below code in new line
export JAVA_HOME=enter_java_path_here
export PATH=$JAVA_HOME/bin:$PATH
eg: export JAVA_HOME=/home/pranav/jdk1.8.0_131
export PATH=$JAVA_HOME/bin:$PATH
Save the file
Type
source ~/.bashrc
in terminal
Done
To set up system wide scope you need to use the
/etc/environment file sudo gedit /etc/environment
is the location where you can define any environment variable. It can be visible in the whole system scope. After variable is defined system need to be restarted.
EXAMPLE :
sudo gedit /etc/environment
Add like following :
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
JAVA_HOME="/opt/jdk1.6.0_45/"
Here is the site you can find more : http://peesquare.com/blogs/environment-variable-setup-on-ubuntu/
How to install java packages:
Install desired java version / versions using official ubuntu packages, which are managed using alternatives:
sudo apt install -y openjdk-8-jdk
or/and other version:
sudo apt install -y openjdk-11-jdk
Above answers are correct only when you have only one version for all software on your machine, and you can skip using update-alternatives. So one can quickly hardcode it in .bashrc or some other place:
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
but it's not healthy, as later on you may change the version.
Correct way to set JAVA_HOME (and optionally JAVA_SDK, JAVA_JRE )
The correct way (and mandatory when you have more than one), is to detect what update-alternative is pointing to, and always use update-alternatives to switch active version.
Here are the suggestions for both: only specific unix account or for all accounts (machine level).
1. for a specific unix account only:
Use this if you don't have permissions to do it at machine level.
cat <<'EOF' >>~/.bashrc
export JAVA_HOME=$(update-alternatives --query java | grep Value | cut -d" " -f2 | sed 's!\(\/.*\)jre\(.*\)!\1!g')
export JDK_HOME=${JAVA_HOME}
export JRE_HOME=${JDK_HOME}/jre/
EOF
2. To do it at machine level, and for all bourne shells, you need 2 steps:
2.a
cat <<'EOF' | sudo tee /etc/profile.d/java_home_env.sh >/dev/null
export JAVA_HOME=$(update-alternatives --query java | grep Value | cut -d" " -f2 | sed 's!\(\/.*\)jre\(.*\)!\1!g')
export JDK_HOME=${JAVA_HOME}
export JRE_HOME=${JDK_HOME}/jre/
EOF
As your shell might not be set as interactive by default, you may want to do this also:
2.b
cat <<'EOF' | sudo tee -a /etc/bash.bashrc >/dev/null
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
EOF
PS: There should be no need to update the $PATH, as update-alternatives takes care of the link to /usr/bin/.
More on: https://manpages.ubuntu.com/manpages/trusty/man8/update-alternatives.8.html
It should put java in your path, probably in /usr/bin/java. The easiest way to find it is to open a term and type which java.
Create/Open ~/.bashrc file $vim ~/.bashrc
Add JAVA_HOME and PATH as referring to your JDK path
export JAVA_HOME=/usr/java/<your version of java>
export PATH=${PATH}:${JAVA_HOME}/bin
Save file
Now type java -version it should display what you set in .bashrc file.
This will persist over sessions as well.
Example :
Update bashrc file to add JAVA_HOME
sudo nano ~/.bashrc
Add JAVA_HOME to bashrc file.
export JAVA_HOME=/usr/java/<your version of java>
export PATH=${PATH}:${JAVA_HOME}/bin
Ensure Java is accessible
java -version
In Case of Manual installation of JDK, If you got an error as shown below
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object
Execute the following command in your JAVA_HOME/lib directory:
unpack200 -r -v -l "" tools.pack tools.jar
Execute the following commands in your JAVA_HOME/jre/lib
../../bin/unpack200 rt.pack rt.jar
../../bin/unpack200 jsse.pack jsse.rar
../../bin/unpack200 charsets.pack charsets.jar
Ensure Java is accessible
java -version
I have a Linux Lite 3.8 (It bases on Ubuntu 16.04 LTS) and a path change in the following file (with root privileges) with restart has helped.
/etc/profile.d/jdk.sh
Step1:
sudo gedit ~/.bash_profile
Step2:
JAVA_HOME=/home/user/tool/jdk-8u201-linux-x64/jdk1.8.0_201
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH
Step3:
source ~/.bash_profile
Let me simplify:
download JDK from Oracle Website : Link
Extract it
Create a folder (jvm) in /usr/lib/ i.e /usr/lib/jvm
move the extracted folder from the jdk to /usr/lib/jvm/
*Note : use terminal, sudo, mv command i.e. sudo mv
Create a .sh file at /etc/profile.d/ eg: /etc/profile.d/myenvvar.sh
In the .sh file type
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0
export PATH=$PATH:$JAVA_HOME/bin
*Note : use terminal, gedit and sudo eg: sudo gedit myenvvar.sh
Turn Off the Computer, after all these steps and Restart it
Open Terminal , and type
java -version
Check the output , then type
echo $JAVA_HOME
Once I've installed the openjdk version of the Java Development Kit on an Ubuntu machine, I use this procedure to create a JAVA_HOME environment variable that doesn't need to be changed after every version upgrade of the openjdk installation.
Firstly, I issue a command to discover the directory in which the java executables are located for this java installation.
echo $(readlink -e `which java` | xargs -0 dirname)
If I'm happy with the output from that, everything else can be derived from it.
Configuring the JAVA_HOME and PATH environment variables
Rather than adding more and more configurations to the ~/.bashrc file I've found it a cleaner practice to create a separate small file that ~/.bashrc can "include" when it runs.
Let's call that file ~/.java_env_vars (but you could name it whatever you wish).
Add an "include condition" to the ~/.bashrc file
Open ~/.bashrc in any text editor and these lines to the end of the file:
# include the java environment configuration file here (if it exists)
if [ -f "$HOME/.java_env_vars" ]; then
. $HOME/.java_env_vars
fi
Create the Java environment variable configuration file
Open any text editor, create the Java environment configuration file ~/.java_env_vars and add this content to it:
#1. set a java_bin variable to the directory containing the actual Java executables.
java_bin=$(readlink -e `which java` | xargs -0 dirname)
#2. append "$java_bin" to the PATH environment variable
export PATH=$PATH:"$java_bin"
#3. assign the directory of the current Java installation to the JAVA_HOME environment variable.
export JAVA_HOME=$(dirname "$java_bin")
NOTE: exporting the PATH and JAVA_HOME variables just ensures that they're always available wherever they're needed.
Source your ~/.bashrc file (so that the changes you made to it are reflected) using the following command:
source ~/.bashrc
Test the Java environment variable configuration
Open a new terminal console, and test the new Java environment variables by issuing this command:
echo "$PATH" && echo "$JAVA_HOME"
Output should be two lines, something like this:
/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-11-openjdk-amd64/bin
/usr/lib/jvm/java-11-openjdk-amd64
All you have to do now is to set the “JAVA_HOME” and “PATH” environment variables and then you are done. Enter the following commands to set your environment variables. Make sure that your environment variables point to a valid installation of JDK on your machine. For Ubuntu 18.04, the path is /usr/lib/jvm/java-8-openjdk-amd64/
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
To check whether your JAVA_HOME path has been successfully saved, enter the following command to check.
echo $JAVA_HOME
Open file /etc/environment with a text editor
Add the line JAVA_HOME="[path to your java]"
Save and close then run source /etc/environment
Set java version from the list of installed. For see the list of the installed version run following command:
update-java-alternatives --list
Then set your java version according to the following command:
sudo update-java-alternatives --set /usr/lib/jvm/java-1.8.0-openjdk-amd64
open jdk once installed resides generally in your /usr/lib/java-6-openjdk
As usual you would need to set the JAVA_HOME, classpath and Path.
In ubuntu 11.04 there is a environment file available in /etc where you need to set all the three paths. And then you would need to restart your system for the changes to take effect..
Here is a site to help you around
http://aliolci.blogspot.com/2011/05/ubuntu-1104-set-new-environment.html
Once JDK installed set the JAVA_HOME in environment
sudo nano /etc/environment and add the line JAVA_HOME="/usr/lib/jvm/jdk-11.0.1/"
Add the configuration in .bashrc
sudo nano ~/.bashrc and add following lines
JAVA_HOME=/usr/lib/jvm/jdk-11.0.11/
PATH=$JAVA_HOME/bin:$PATH
refresh the new configuration with source ~/.bashrc
enter the command java -version and you can see the version installed on your machine
You can install the default Ubuntu(17.10) java from apt:
sudo apt install openjdk-8-jdk-headless
And it will set the PATH for you, if instead you need to install specific version of Java you can follow this YouTube
I installed java 11 in my Ubuntu 20.04. Setting up a JAVA_HOME for the same.
enter the this command to find out your ubuntu version --
swapnil#swapnil-vm:~$ lsb_release -d
Description: Ubuntu 20.04.3 LTS
enter this command to find out the location of your jvm --
swapnil#swapnil-vm:~$ whereis jvm
jvm: /usr/lib/jvm
open .bashrc in any editor of your choice --
nano .bashrc
add the following lines --
## setting JAVA_HOME
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH
now you are good to go!!
open a new terminal and enter the command --
ehco $JAVA_HOME
Use the following lines to set the path variables in the /etc/environment
echo export JAVA_HOME=/path/to/java | sudo tee -a /etc/environment
echo export JRE_HOME=/path/to/jre | sudo tee -a /etc/environment
It should work.
Note:
You should reboot the system for changes to take effect.
Installation of Oracle Java:
Download the tarball (.tar file) from Oracle website
unzip it by sudo tar -xvpzf fileName -C /installation_folder_name
change the files permission and ownership
add the following two lines in /etc/profile
export JAVA_HOME=/home/abu/Java/jdk1.8.0_45/
export PATH=$JAVA_HOME/bin:$PATH
restart the machine and check by java -version and javac -version
First, check whether env var exists or not
echo $JAVA_HOME
if an env var exists with that name then the above command will return the env var Path. if it's return nothing then copy the env path first then execute below command. such as my java env path is /usr/lib/jvm/java-11-openjdk-amd64
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

having a trouble setting java_home in ubuntu

hello am trying to set environment for react native development, i followed all the steps in the get started guide, am trying to run the project on a Genymotion emulator, when i try to start the project i get an error message saying that i didn't set the java_home path, i watched a tutorial on how to do that, i think my problem is that am setting the path wrong... this is the path to java exe files from the tutorials :
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/java/jdk1.7.0_65/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/java/jdk1.7.0_65/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/java/jdk1.7.0_65/bin/javaws"
the path to those files in my computer is this :
/usr/lib/java/jdk-12.0.2_linux-x64_bin/data/usr/lib/jvm/jdk-12.0.2/bin$
so am wondering what to change.
..... this is a screenshot of ls "bin" !
To set JAVA_HOME environment variable, do the following:
Launch Terminal by pressing Ctrl+Alt+T on your keyboard.
Enter the following command:
$ gksudo gedit /etc/environment
Depending on where you installed your Java, you will need to provide the full path. For this example, I installed Oracle JDK 7 in the /usr/lib/jvm/java-7-oracle directory.Scroll to the end of the file and enter the following:
JAVA_HOME=/usr/lib/jvm/java-7-oracle
export JAVA_HOME
Save your file and exit gedit.
Lastly, reload the system PATH with the following command:
$ . /etc/environment
The above method will save you the hassle in having to run the commands every time you log in to your computer.

"JAVA_HOME is set to an invalid directory" gradlew error on Fedora linux

I installed Oracle JDK specially to work in Android studio, but it doesn't work for some reason:
ERROR: JAVA_HOME is set to an invalid directory: /usr/java/jdk-10.0.2/
I already selected this java version in alternatives list. I also tried to use /java/latest, but the same error occurs. JAVA_HOME is set in ~/.bash_profile. I tried both setting it directly to the java location:
export JAVA_HOME=/usr/java/jdk-10.0.2/
and by the code below:
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
None of these worked.
Yeah it is wrong path.
You didn't put "/" before bin it should be either
export JAVA_HOME=$(readlink -f /usr/bin/java/ | sed "s:bin/java::")
or
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::")
as per oracle documentation
Install the JDK software.
Go to http://java.sun.com/javase/downloads/index.jsp.
Select the appropriate JDK version and click Download.
The JDK software is installed on your computer, for example, at /usr/jdk/jdk1.6.0_02. You can change this location.
Set JAVA_HOME.
Korn and bash shells:
export JAVA_HOME=jdk-install-dir
export PATH=$JAVA_HOME/bin:$PATH
Bourne shell:
JAVA_HOME=jdk-install-dir
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
C shell:
setenv JAVA_HOME jdk-install-dir
setenv PATH $JAVA_HOME/bin:$PATH
export PATH=$JAVA_HOME/bin:$PATH
Change the permissions to enable you to run the GlassFish ESB Installer.
chmod 755 JavaCAPS.bin

setting JAVA_HOME in in OS X Yosemite 10.10.4

trying to set JAVA_HOME to execute maven but there is no way
$ which java
/usr/bin/java
$ export JAVA_HOME=/user/bin/
$ mvn
Error: JAVA_HOME is not defined correctly.
We cannot execute /user/bin//bin/java
$ export JAVA_HOME=/Library/Java
$ mvn
Error: JAVA_HOME is not defined correctly.
We cannot execute /Library/Java/bin/java
You can use something like this - but substitute the java version with the one actually on your machine:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

How to install Maven on OSX 10.10 (Yosemite)?

I am trying to install Maven on my Mac, but I can not get the JAVA_HOME variable right. The path that the maven website gives is,
usr/java/jdk1.7.0_51
however the java folder does not exist in /usr/ for me. I installed the JRE and JDK, nothing changed. How can I fix this?
Using brew - brew install maven - that's it.
Installing Maven (for me) usually involves these steps (apart from having a working java installed):
Download Maven from http://maven.apache.org/download.cgi
Unpack to /path/to/maven
Set the following in .bashrc
export M2_HOME=/path/to/maven
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
Reload bash, and now "mvn --version" should be fine. Make sure to not have any reference to JAVA_HOME in your .bashrc (or .bash_aliases) if you havent already set it to somewhere you placed your jdk.
In my case, none of the exported variables saved after bash quit. Also, export M2=$M2_HOME/bin; export PATH=$M2:$PATH into bash didn't help, either. So, I had to write absolute paths into bash profile. Here are my steps:
1) open bash
2) next two steps are required to correctly create paths in your system. Type in (line by line)
JAVA_HOME=/usr/libexec/java_home
export M2_HOME=/path/to/your/apache-maven-3.x.x
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
3) than echo all four variables
echo $JAVA_HOME
echo $M2_HOME
echo $M2
echo $PATH
4) open second window of bash for your convenience.
5) choose root folder (type cd without anything and press enter)
cd
6) if you don’t have file called «.bash_profile» than just create it
touch .bash_profile
7) open edit your «.bash_profile»
nano .bash_profile
5) copy results of echo commands to your «.bash_profile». it should look something like.
JAVA_HOME=/usr/libexec/java_home
M2_HOME=/path/to/your/apache-maven-3.x.x
M2=/path/to/your/apache-maven-3.x.x/bin
PATH=/path/to/your/apache-maven-3.x.x/bin:/path/to/maven/bin:/$….bla-bla-bla…
6) after edit save by pressing ctrl+s and close by ctrl+x
For Macports : sudo port install maven3
Change maven3 to maven1 or maven2 for corresponding version number.
On Yosemite, I've not had JAVA_HOME defined and I have version '1.7.0_65'. I also have maven installed and use it nearly every day. Installation of maven is performed by the following steps:
curl http://download.nextag.com/apache/maven/maven-3/3.2.3/binaries/apache-maven-3.2.3-bin.tar.gz | tar pzxvf -
set the MAVEN_HOME environment variable to:
pwd/apache-maven-3.2.3
add MAVEN_HOME to your path.
Now you should be golden. Leave a comment if you aren't sorted.
Sorry this is late.
I installed JDK and Maven following instructions from each of those sites, then I had to edit my .profile file to get the PATH variables set.
$ sudo vim .profile
Then I added the following to my .profile (i for insert mode)
# JDK configuration
#
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH
#
# Maven configuration
#
MAVEN_3_3_3=/opt/maven/apache-maven-3.3.3
export MAVEN_3_3_3
M2_HOME=$MAVEN_3_3_3
export M2_HOME
PATH=$PATH:$M2_HOME/bin
export PATH
Next you write and quit vim (:wq), quit and reopen terminal and type mvn --version to test your install.
Your output should look like this:
$ mvn --version
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T06:57:37-05:00)
Maven home: /opt/maven/apache-maven-3.3.3
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.10.5", arch: "x86_64", family: "mac"
Note: you may need to adjust the paths to your jdk and maven installations.

Categories