Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure PATH and CLASSPATH for Java in Mac OS

Tags:

java

macos

I am trying to run Java programs on Mac OS. I installed JDK version 1.7 for Mac and even though, I am getting errors in executing Java program. So, I got to know that I have to set PATH and CLASSPATH (Environment variables) in Mac to get the Java programs run successfully. Please help me with the issue.

like image 223
VAMSHI PAIDIMARRI Avatar asked Sep 30 '22 18:09

VAMSHI PAIDIMARRI


1 Answers

First, you need to find where the bin directory that contains all java binary files is located. To do that simply

cd /Library/Java/

Then do one "ls" to see what you have in this directory. Then continue cd to the inner directories to reach to the bin file. It might be a little different from me but my bin directory is in this path:

/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/

In fact, Home is the path of the directory that contains the subdirectory named "bin" which contains all the java binary files needed for running java. This is the path that needs to be defined in PATH environment. keep this path.

Second, we need to check whether we have the.bash_profile or not. Simply cd to the home (I mean the user directory) -- in any path if you just type cd and press enter, you can go to the home. When you are located in home, type this:

cat .bash_profile

if you see the error regarding a problem in finding the file then you need to create this file. For creating this file type this:

nano .bash_profile

you can see that the editor for writing into this file is open now. Then simply write this:

JAVA_HOME= /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/
export JAVA_HOME;

and ^O then enter then ^X to save this file.

Third, exit the terminal and reopen it and for testing whether you set the path environment or not write this comment in the terminal:

 $JAVA_HOME/bin/java -version

If you see the correct version of java is showing up --Success!

Cite: http://www.sajeconsultants.com/how-to-set-java_home-on-mac-os-x/

like image 161
Reihan_amn Avatar answered Oct 06 '22 00:10

Reihan_amn