A place to breathe

Friday, December 7, 2012

Pointing the correct executable from JAVA_HOME environment on Unix/Linux using Bash Shell

AAaaah. That JAVA_HOME environment again.

When I was student (and those days, knows nothing about environment variables), setting JAVA_HOME (and CLASSPATH) environment is, to say the lest, difficult and confusing.

Anyway, it's actually very simple idea. Every machines has their own java environment and by default, it is usually installed in /usr/bin/java

Run the following to test:

$> which java 
 /usr/bin/java 

Now, we don't usually want this default installation because it's old , etc.

Say, you already installed a new JDK and it's located here: /opt/jdk_1.6/

And you want to use those executables. This is how you do it:

1. Login to your Bash (I'm talking about Unix/Linux here. So Windows user... there's a way to do it through GUI not covered here)

2. Open your .bash_profile

$> vi .bash_profile  (you can use any editor you want)

3. add the following command:

JAVA_HOME=/opt/jdk_1.6  
Comment: This sets the environment variable "JAVA_HOME" to be that path)

PATH=${JAVA_HOME}/bin:$PATH 
Comment: Add the "bin" executable that points to the new "java" executable and other more executables to the new one, into the existing PATH)
IMPORTANT:: Put your $JAVA_HOME/bin  at the front so that the console search the new path first to find the executable. Otherwise, if it found in the /usr/bin/, it'll take that one and you'll get the old /usr/bin/java )

export $PATH 
Comment: Add the new path as the path


OK, that's it.

Once you finish that, source  it to get the new changes:

$>source .bash_profile 

$> which java 
/opt/jdk_1.6/bin/java 

TADAAA. It should point to the new location of our freshly installed Java directory in /opt/jdk_1.6

About Me

I'm currently a software engineer. My specific interest is games and networking. I'm running software company called Nusantara Software.