Setting of JAVA_HOME and PATH in Linux
The JAVA_HOME environment variable points to the JDK installation directory and used by many Java tools and applications like Eclipse, Maven, ANT, Tomcat etc. to figure out Java executables as they need Java for running. Unfortunately, When you install Java on Linux or Windows, this environment variable is not set automatically. You need to do it yourself. Java developers like me always set useful environment variables like JAVA_HOME, PATH, and CLASSPATH on the login scripts executed by the shell when you log in to Linux e.g. bash_profile.sh, but you can also set JAVA_HOME into /etc/profile for all users. I'll show you steps how to find the location of Java and how to setup the JAVA_HOME in Linux in this article and these steps should also work with any Linux or Java version e.g. Ubuntu, RedHat, CentOS, SUSE, ArchLinux, Debian, Fedora etc.
Steps to set JAVA_HOME in Linux
1) The first step is to find the location where Java is installed on your Linux machine. To check that open putty and connect to your Linux server and type the following command$ java Usage: java [-options] class [args...] (to execute a class) or java [-options] -jar jarfile [args...] (to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to select the "server" VM
It means Java is installed and added to your PATH, so half of the job is done already. All you need to is find the location of JDK, which you can find either by printing PATH as follows
$ echo $PATH /usr/local/bin:/bin:/usr/bin:/usr/sun/jdk/v1.6.0_16-64bit/bin:.
or
$ which java usr/sun/jdk/v1.6.0_16-64bit
You can see that JDK is installed on "/usr/sun/jdk/v1.6.0_16-64bit".
2) Add the following line of your .bash_profile file if you want to set JAVA_HOME for yourself or /etc/profile if you want to set JAVA_HOME for all user
vim ~/bash_profile export JAVA_HOME=/usr/sun/jdk/v1.6.0_16-64bit/ export PATH = $JAVA_HOME/bin:$PATH
or
vim /etc/profile export JAVA_HOME=/usr/sun/jdk/v1.6.0_16-64bit/ export PATH = $JAVA_HOME/bin:$PATH
That's it you are done. Though don't forget to open another shell window by typing bash, csh, or ksh. The new environment variable will not be added to existing shell, you need to open a new shell window.
Also, if you use bash shell than adding into ~/.bash_profile make sense, if you use csh (C shell) then add JAVA_HOME into ~/.csh_profile and similarly for ksh (K shell) add JAVA_HOME into ~/.ksh_profile.
They are hidden files in your home directory in Linux, that's why they had prefix dot (.) in their name. You can use "ls - alrt" command to see them.
Here is a sample /etc/profile file with JAVA_HOME and PATH variable defined for your reference:
By the way, if you don't see Java installed on your machine, then download the Linux version of Java from Oracle's website and install it. Once you are done with installation just find the folder where JDK is installed and follow the steps given above to set JAVA_HOME for your Linux environment.
For Application, JAVA_HOME is generally referred on start script. It's bad practice to refer hard coded path of Java installation in scripts, you will never find Tomcat, Maven, Eclipse, NetBeans or IntelliJ doing that because then when you update to a new Java version they won't be able to use it until you change their start script. Using JAVA_HOME makes it easy to switch to a different or new Java version. See Core Java for the Impatient to learn more about basic concepts of core Java.
Why should you set JAVA_HOME in Linux
Even though many developers knows about JAVA_HOME they don't set in their environment due to various reasons. I strongly recommend you to set JAVA_HOME, PATH, and CLASSPATH for your development environment. Here are some reasons why you set JAVA_HOME in Linux1) It's easy to upgrade JDK without affecting your application startup and config file which points to JAVA_HOME. you just need to download a new version and make sure your JAVA_HOME points to the new version of Java. This is the best benefit of using an environment variable or links.
2) The JAVA_HOME variable is short and concise instead of the full path to JDK installation directory.
3) JAVA_HOME variable helps your program achieve platform independence i.e. if your startup script uses JAVA_HOME then it can run on Windows and UNIX without any modification, you just need to set JAVA_HOME on the respective operating system.
4) The JAVA_HOME is standard, which means other tools which need Java e.g. Maven, Eclipse can refer this variable without having any knowledge of where Java is physically installed on your machine, which is obviously different in different user's machine. JAVA_HOME allows everybody to access Java in a common and standard way.
I also suggest reading Core Java Volume 1 - Fundamentals by Cay S. Horstmann to learn more about Java fundamentals like this one.
That's all about how to set JAVA_HOME environment variable in Linux. I have also touched base on why you should do it. If you face any problem while setting and using the JAVA_HOME environment variable, feel free to ask. I'll be glad to help you out.
0 comments:
Post a Comment