//
you're reading...
Java Programming

Java JDK Environment Variables Configuration in Linux (Centos)

Environment Variables need to be configured

PATH environment variable. Role is specified command search path, while executing a command in shell, it will go to the PATH variable to find the path specified to see if it could find the appropriate command. We need to add the bin folder under the JDK directory to the existing PATH variable. The bin folder contains executable programms often include javac, java and javadoc. Once the PATH variable configured, you can execute any programm(such as java, javac) under the shell

CLASSPATH environment variable. Role is to specify the class search path to use already written class. JVM is through CLASSPTH to find classes it need. We need to add the dt.jar and tools.jar in the lib of the JDK to the CLASSPATH; and of course, the current directory “.” also need to be added to the variable as well.

JAVA_HOME environment variable. It points to the JDK installation directory, Eclipse/NetBeans/Tomcat software is through the search JAVA_HOME variable to find and use the installed jdk.

Three ways to configue the environment variables

First approach, this is the recommended method if your computer just used as development because all users have the right to use the shell environment variables that may give the system security issues. Edit the /etc/profile, add the follow contents at the end.

export JAVA_HOME=/usr/share/jdk1.7.0_14
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 

Re-login, then the environment will apply.

Note:

  • Change the /usr/share/jdk1.7.0_14 to JDK installation directory
  • Under linux, it use “:” to separate the path.
  • $PATH, $CLASSPATH and $JAVA_HOME is used to refer to the original value of the environment variable. When configure the environment variables particular to be careful not to overwrite the original value, which is a common mistakes.
  • CLASSPATH in the current directory – “.“, don’t delete it
  • export command is to export these three variables as global variables.
  • We must strictly distinguish between the case.

Second approach, this method is more secure, it can control the permission for using these environmental variables on user level, if you need to use these environmental variables to a user permissions, you only need to modify the .bash_profile(or .profile if you use C shell) file in their personal user home directory.

At the end of the .bash_profile, add following contents.

export JAVA_HOME=/usr/share/jdk1.7.0_14
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 

Re-login, then the environment will apply.

Third approach, configure the environment variables under the shell directly. This approach is not recommanded, because if you open a new login session, all three environment variables will not be valided.

To do this, only thing need to do is to open a new session windows and export the environment variables.

[root@test ~]# export JAVA_HOME=/usr/share/jdk1.6.0_14
[root@test ~]# export PATH=$JAVA_HOME/bin:$PATH
[root@test ~]# export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
Advertisements

About daviewning

I am an Oracle DBA

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: