How to set JAVA_HOME environment variable on macOS? - bash

According to this mobilefirst tutorial, it mentioned:
You must have the JAVA_HOME environment variable set to your JDK directory.
For example:
Mac OSX: /Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home
I've added this 2 lines in .bash_profile:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
export PATH=$PATH:$JAVA_HOME/Contents/Commands
Is this correct?
Thanks.

in .bash_profile:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.xx/Contents/Home

Adding the below answer to help those who are looking for step by step instructions on how to setup Java_Home on a Mac.
Determine whether Java is installed by using the command below:
which java
You will something like this - /usr/bin/java
Next Step will be to determine the version of Java Installed by using the command below:
java -version
You will see something like java version "1.8.0_131"
Next step will be to get the location where the Java is installed:
cd /Library/Java
Under the Java folder, select the folder with the version that was displayed earlier:
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
Next check if Java_Home is setup to the correct location:
echo $JAVA_HOME
It will result in blank output if it is not already setup. If it returns the location, check if it points to the correct folder.
You can add or update the Java_Home using the below commands:
vi ~/.bash_profile
Navigate to the end of the file by pressing "Shift + g".
Now press "i" to get to insert mode.
Add the below lines in the bash_profile after replacing the path to the java home directory on your mac
# Setting Java_Home
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
Save this by pressing Esc followed by ":wq!"
Finally open a new terminal window and test the variable is setup correctly:
echo $JAVA_HOME
It should return the Java_Home path setup

If your default terminal is /bin/zsh (Z Shell) like in my case, then you should set these environment variable in ~/.zshenv file with following contents:
export JAVA_HOME="$(/usr/libexec/java_home)"
Similarly, any other terminal type not mentioned above, you should set environment variable in its respective terminal env file.
After saved the content into env file, restart terminal and call following commands:
echo $JAVA_HOME
It should display the full Java path.

Just a note to say this still works in OpenJDK version 11. As shown in the original question the java_home utility lives in /usr/libexec, which might not be on your $PATH. You can run the command directly in a terminal window, here's sample output too:
$ /usr/libexec/java_home -v 11
/Library/Java/JavaVirtualMachines/jdk-11.0.4.jdk/Contents/Home
And set JAVA_HOME to that value. Even better, wire your shell script's dot files as suggested in the original question by #user1872384, because that way your environment automatically keeps up with JDK upgrades as you install them.

Related

How to access Java 9 Jshell on mac

I have installed Jdk9 on MAC OSX and I have added the JDK path to bash_profile. When I use jshell command from terminal, I am getting 'Command not found' error. I am able to get the correct version number when I type command java -version.
How do I access the jshell from terminal? do i need to include jdk 9 bin folder path in bash_profile?
Add JDK-9 bin in PATH environment variable.
export PATH=/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin:$PATH
jshell
| Welcome to JShell -- Version 9.0.1
| For an introduction type: /help intro
jshell>
Yes. All you need to do is configure the $JAVA_HOME/bin in your bash_profile which can be done as :
and then confirm the java -version on your prompt.
PS: All other variables in bash_profile except JAVA_HOME and PATH in the profile can be ignored.
Short Answer
Add the following to your .bash_profile
export PATH=$PATH:/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin
After restarting your terminal, jshell should be a recognized command.
Detailed Explanation
Adding the directory /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin to our $PATH,
allows us to be in any directory and
execute scripts/commands located in /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin
$PATH is an environment variable
Executing echo $PATH in the terminal will print a colon-delimited list of directories stored in the $PATH environment variable.
For the following examples, directory = whatever directory you want to add to your $PATH
PATH=directory:$PATH --> prepends a directory to the colon-delimited list
whereas, PATH=$PATH:directory --> appends a directory to the colon-delimited list

Unable to export environment variables in cygwin within a shell script

I created a setenvj1.6 shell script in my /usr/local/bin directory, in cygwin (Windows 7).
setenvj1.6:
export PATH=/cygdrive/c/bea/bea10/bea10g3/jdk160_05/bin:$PATH
export JAVA_HOME=/cygdrive/c/bea/bea10/bea10g3/jdk160_05
echo Set for Java 1.6
when I run setenvj1.6, like this:
myuser#mycomp /usr/local/bin
$ setenvj1.6
Set for Java 1.6
I see that the script has run, but when I check if anything is set correctly, I see that my variables are not set:
myuser#mycomp /usr/local/bin
$ echo $JAVA_HOME
Similarly, if I echo $PATH, I get my usual PATH variable but without the directory I tried to add with export.
How do I get this to work?
I need a bash script like the one I specified that I can run after I've run cygwin so that I can change my paths to whatever I want. Thanks.

mvn command not found in OSX Mavericks

Before marking this as duplicate, I went through these posts, but nothing helped.
'mvn' is not recognized as an internal or external command,
Getting -bash: mvn: command not found,
Can't access mvn command from command line?
Some are specific to windows and did not help. A couple of them on Mac OS X gave suggestions, that I tried but did not help.
What I tried (this is exactly what Maven suggests):
Extract the distribution archive, i.e. apache-maven-3.1.1-bin.tar.gz
to the directory you wish to install Maven 3.1.1. These instructions
assume you chose /usr/local/apache-maven. The subdirectory
apache-maven-3.1.1 will be created from the archive. In a command
terminal, add the M2_HOME environment variable, e.g. export
M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1. Add the M2
environment variable, e.g. export M2=$M2_HOME/bin. Optional: Add the
MAVEN_OPTS environment variable to specify JVM properties, e.g. export
MAVEN_OPTS="-Xms256m -Xmx512m". This environment variable can be used
to supply extra options to Maven. Add M2 environment variable to your
path, e.g. export PATH=$M2:$PATH. Make sure that JAVA_HOME is set to
the location of your JDK, e.g. export JAVA_HOME=/usr/java/jdk1.5.0_02
and that $JAVA_HOME/bin is in your PATH environment variable. Run mvn
--version to verify that it is correctly installed.
I see that on the terminal that I used for installation, it works fine. I do not have this issue. but when I tried on a new terminal, I get command not found.
I also added export PATH=$M2 to my .bashrc, I did source and then restarted the terminal, still it did not help.
can someone suggest how to make it available in all sessions of terminal?
Thanks
Try following these if these might help:
Since your installation works on the terminal you installed, all the exports you did, work on the current bash and its child process. but is not spawned to new terminals.
env variables are lost if the session is closed; using .bash_profile, you can make it available in all sessions, since when a bash session starts, it 'runs' its .bashrc and .bash_profile
Now follow these steps and see if it helps:
type env | grep M2_HOME on the terminal that is working. This should give something like
M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1
typing env | grep JAVA_HOME should give like this:
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home
Now you have the PATH for M2_HOME and JAVA_HOME.
If you just do ls /usr/local/apache-maven/apache-maven-3.1.1/bin, you will see mvn binary there.
All you have to do now is to point to this location everytime using PATH. since bash searches in all the directory path mentioned in PATH, it will find mvn.
now open .bash_profile, if you dont have one just create one
vi ~/.bash_profile
Add the following:
#set JAVA_HOME
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home
export JAVA_HOME
M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1
export M2_HOME
PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin
export PATH
save the file and type source ~/.bash_profile. This steps executes the commands in the .bash_profile file and you are good to go now.
open a new terminal and type mvn that should work.
Solutions above are good but they require ~/.bash_profile. /usr/local/bin is already in the $PATH and it can be confirmed by doing echo $PATH. Download maven and run the following commands -
$ cd ~/Downloads
$ tar xvf apache-maven-3.5.3-bin.tar.gz
$ mv apache-maven-3.5.3 /usr/local/
$ cd /usr/local/bin
$ sudo ln -s ../apache-maven-3.5.3/bin/mvn mvn
$ mvn -version
$ which mvn
Note: The version of apache maven would be the one you will download.
Here is what worked for me.
First of all I checked if M2_HOME variable is set env | grep M2_HOME. I've got nothing.
I knew I had Maven installed in the folder "/usr/local/apache-maven-3.2.2", so executing the following 3 steps solved the problem for me:
Set M2_HOME env variable
M2_HOME=/usr/local/apache-maven-3.2.2
Set M2 env variable
M2=$M2_HOME/bin
Update the PATH
export PATH=$M2:$PATH
As mentioned above you can save that sequence in the .bash_profile file if you want it to be executed automatically.
I got same problem, I tried all above, nothing solved my problem. Luckily, I solved the problem this way:
echo $SHELL
Output
/bin/zsh
OR
/bin/bash
If it showing "bash" in output. You have to add env properties in .bashrc file (.bash_profile i did not tried, you can try) or else
It is showing 'zsh' in output. You have to add env properties in .zshrc file, if not exist already you create one no issue.
The possible solution can be that maven is not installed in your mac system.
Use this command to install maven:
brew install maven
And, to verify, that it is successfully installed, run this command:
mvn -v
If it returns you maven version, then maven is successfully installed in your system.
steps to install maven :
download the maven file from http://maven.apache.org/download.cgi
$tar xvf apache-maven-3.5.4-bin.tar.gz
copy the apache folder to desired place $cp -R apache-maven-3.5.4 /Users/locals
go to apache directory $cd /Users/locals/apache-maven-3.5.4/
create .bash_profile $vim ~/.bash_profile
write these two command :
export M2_HOME=/Users/manisha/apache-maven-3.5.4
export PATH=$PATH:$M2_HOME/bin
7 save and quit the vim :wq!
restart the terminal and type mvn -version
I followed brain storm's instructions and still wasn't getting different results - any new terminal windows would not recognize the mvn command. I don't know why, but breaking out the declarations in smaller chunks .bash_profile worked. As far as I can tell, I'm essentially doing the same thing he did. Here's what looks different in my .bash_profile:
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home
export PATH JAVA_HOME
J2=$JAVA_HOME/bin
export PATH J2
M2_HOME=/usr/local/apache-maven/apache-maven-2.2.1
export PATH M2_HOME
M2=$M2_HOME/bin
export PATH M2
You probably have 2 types of shell instances.
sh vs zsh.
Both can have different path defined.
Check your PATH environment variable by typing the below line in terminal
echo $PATH
To test you can change shell mode -
sh to zsh -> type zsh and press enter in terminal (notice $ changes to %)
zsh to sh -> type sh/bash and press enter in terminal (notice % changes to $)
In Both shell modes check for PATH env.
Make both same, or append path from other as needed.
Commands running in 1 shell and not in other would be sorted.
For some of you the cause might be using of other variables in the path to the maven directory. More details in the answer.

Working With Hadoop: localhost: Error: JAVA_HOME is not set

I'm working with Ubuntu 12.04 LTS.
I'm going through the hadoop quickstart manual to make a pseudo-distributed operation. It seems simple and straightforward (easy!).
However, when I try to run start-all.sh I get:
localhost: Error: JAVA_HOME is not set.
I've read all the other advice on stackoverflow for this issue and have done the following to ensure JAVA_HOME is set:
In /etc/hadoop/conf/hadoop-env.sh I have set
JAVA_HOME=/usr/lib/jvm/java-6-oracle
export JAVA_HOME
In /etc/bash.bashrc I have set
JAVA_HOME=/usr/lib/jvm/java-6-oracle
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH
which java returns:
/usr/bin/java
java –version works
echo $JAVA_HOME returns:
/usr/lib/jvm/java-6-oracle
I've even tried becoming root and explicitly writing the in the terminal:
$ JAVA_HOME=/usr/lib/jvm/java-6-oracle
$ export JAVA_HOME
$ start-all.sh
If you could show me how to resolve this error it would be greatly appreciated.
I'm thinking that my JAVA_HOME is being overridden somehow. If that is the case, could you explain to me how to make my exports global?
I am using hadoop 1.1, and faced the same problem.
I got it solved through changing JAVA_HOME variable in /etc/hadoop/hadoop-env.sh as:
export JAVA_HOME=/usr/lib/jvm/<jdk folder>
The way to solve this problem is to export the JAVA_HOME variable inside the conf/hadoop-env.sh file.
It doesn't matter if you already exported that variable in ~/.bashrc, it'll still show the error.
So edit conf/hadoop-env.sh and uncomment the line "export JAVA_HOME" and add a proper filesystem path to it, i.e. the path to your Java JDK.
# The Java implementation to use. Required.
export JAVA_HOME="/path/to/java/JDK/"
Ran into the same issue on ubuntu LTS 16.04. Running bash -vx ./bin/hadoop showed it tested whether java was a directory. So I changed JAVA_HOME to a folder and it worked.
++ [[ ! -d /usr/bin/java ]]
++ hadoop_error 'ERROR: JAVA_HOME /usr/bin/java does not exist.'
++ echo 'ERROR: JAVA_HOME /usr/bin/java does not exist.'
ERROR: JAVA_HOME /usr/bin/java does not exist.
So I changed JAVA_HOME in ./etc/hadoop/hadoop-env.sh to
export JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre/
and hadoop starts fine.
The way to debug this is to put an "echo $JAVA_HOME" in start-all.sh. Are you running your hadoop environment under a different username, or as yourself? If the former, it's very likely that the JAVA_HOME environment variable is not set for that user.
The other potential problem is that you have specified JAVA_HOME incorrectly, and the value that you have provided doesn't point to a JDK/JRE. Note that "which java" and "java -version" will both work, even if JAVA_HOME is set incorrectly.
extract from etc/hadoop/hadoop-env.sh
The only required environment variable is JAVA_HOME. All others are
optional. When running a distributed configuration it is best to
set JAVA_HOME in this file, so that it is correctly defined on
remote nodes.
This means its better and advised to set JAVA_HOME here.. even though the existing definition reads the JAVA_HOME variable. Perhaps its not getting the value of JAVA_HOME from previously set value... standard apache manual does not tell this :( :(
This error is coming from Line 180
if [[ -z $JAVA_HOME ]]; then
echo "Error: JAVA_HOME is not set and could not be found." 1>&2
exit 1
fi
in libexec/hadoop-config.sh.
Try echo $JAVA_HOME in that script. If it doesn't recognize,
Find your JAVA_HOME using this:
$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
and replace the line
export JAVA_HOME=${JAVA_HOME}
in /etc/hadoop/hadoop-env.sh with JAVA_HOME you got from above command.
I also had faced the similar problem in hadoop 1.1
I had not noticed that the JAVA_HOME was commented in: hadoop/conf/hadoop-env.sh
It was
/#JAVA_HOME=/usr/lib/jvm/java-6-oracle
Had to change it to
JAVA_HOME=/usr/lib/jvm/java-6-oracle
regardless of debian or any linux flavor, just know that ~/.bash_profile belongs to specific user and is not system wide.
in pseudo-distributed environment hadoop works on localhost so the $JAVA_HOME in .bash_profile is no use anymore.
just export the JAVA_HOME in ~/.bashrc and use it system wide.
Check if your alternatives is pointing to the right one, you might actually be pointing to a different version and trying to alter the hadoop-env.sh on another installed version.
-alternatives --install /etc/hadoop/conf [generic_name] [your correct path] priority {for further check man page of alternatives}
to set alternatives manually,
alternatives --set [generic name] [your current path].
Change the JAVA_HOME variable in conf/hadoop-env.sh
export JAVA_HOME=/etc/local/java/<jdk folder>
echo "export JAVA_HOME=/usr/lib/java" >> $HADOOP_HOME/etc/hadoop/hadoop-env.sh
Notice: Do not use export JAVA_HOME=${JAVA_HOME} !
I put it on the first line of file ~/.bashrc, then it works well!
export JAVA_HOME=/usr/lib/jvm/default-java

bash error : Cannot find /Library/...../setclasspath.sh, but it's there

I'm trying to set up tomcat server and I am using the instructions from a website that has instructed me to
# Execute the Tomcat startup script
./startup.sh
but when i enter the ./startup.sh command i get an error
Cannot find /Library/Tomcat/Home/bin/setclasspath.sh
but the setclasspath.sh is there (see screenshot below):
What am i doing wrong????
now i cannot cd to Home (see screenshot below):
here is the path info:
I had same problem on my mountain lion machine and I use this command :
$ unset CATALINA_HOME
and after that it solved my problem.This command will unset the environmental variable CATALINA_HOME.
just run:
$ unset CATALINA_HOME
in Terminal, and it works again.
Please RESET the CATALINA_HOME to point to correct path. That should resolved the problem.
for example; currently it might be at "/usr/share/tomcat6/" but you need to verify & point till the correct directory like "/usr/share/tomcat6/apache-tomcat-6.0.37/"
Its because of permission, you can run this using root
$ sudo pathtotomcat/bin/./startup.sh
I too had similar issue and I am able to resolve it by simply changing the file permission of tomcat folder . Make it read and Write for everyone. And sh startup.sh command would work fine with Tomcat/7.0.34
I got this solution from here
To make all scripts runnable:
sudo chmod +x /Library/Tomcat/bin/*.sh
I hope it solves your issue too.
The answer of ali is good, but for some reason I also needed to open .bash_profile and comment out CATALINA_HOME
This happens almost definitely because of incorrect value of CATALINA_HOME environment variable in your current shell. In my case for example I had an older tomcat install that I removed and installed another version but forgot to change the env var set in .bash_profile. So any of the methods suggested which remove the var will work. If you have CATALINA_HOME set in .bash_profile, remove it. unset CATALINA_HOME would work too but if its in profile, it will again be set when you open a new shell.
Although all mentioned answers look fine it's a good idea to be aware the main reason.
Somewhere on your operating system, a file is setting environment variables for your operating system, one of those variable names is CATALINA_HOME which is pointing to an address or path. when you attempt to run ./startup.sh or catalina.sh run, these command needs the CATALINA_HOME which is set by your operating system and will override the default CATALINA_HOME of tomcat configuration. (You may have used tomcat and set this variable before and now you forget it)
So the main solution is to edit that special file and it really depends on operating system. In my case, Fedora, two files are candidate one of them is ~/.bashrc and another one is /etc/profile. After editing them you need to source them or log out and then log in (On OS X I think it should be in /Users/username/.bash_profile file).
of course unset CATALINA_HOME command works but after closing and opening a new terminal, again you need to use this command due to refreshing.
U need to set two lines at two places
path details
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
export CATALINA_HOME=/opt/tomcat/apache-tomcat-7.0.90
put this path at the end of the lines
files that you want to edit
vi ~/.bashrc
vi ~/.bash_profile
./shutdown.sh
./startup.sh
its works for me

Resources