How to configure Java / Javac on Linux

By angsuman, Gaea News Network
Saturday, February 6, 2010

So now you have bravely installed Java on Linux, preferably the Sun’s version, right?

However you may have previous versions of JDK / JRE already on your system, presumably the one bundled with Linux like GCJ JRE.

Most of us are familiar with running java or javac with a simple command line invocation like: java or javac, without bothering about the classpath. Unfortunately you may find that unintended or older versions of java, javac are running despite the fact that you have installed the latest version of Java in your system.
Many poor souls will at this point resort to hardcoding the path to their intended Java executables and making changes in path to point to the correct executables. Sadly it is fraught with danger and prevent generalizing across computers. The second issue is that in some situations you may genuinely want to run alternative versions of a program like the gcj version or IBM JDK for certain applications or even for testing purposes. How can you do that without resorting to painful remapping everytime?

Fortunately Linux comes with an elegant solution which allows you to map a name like java or javac to multiple applications located anywhere on your harddisk. The utility to use is appropriately named alternatives. It is normally located in /usr/sbin/alternatives

When I installed JDK 1.7 / JDK 7 on my system, I faced the same situation. I already had GCJ in my system. This is what I did to add JDK 7 java mapping to my system.

/usr/sbin/alternatives –install /usr/bin/java java /usr/java/latest/bin/java 2

This created a mapping from java to my java executable which is in /usr/java/latest/bin/java
2 indicates the position where I want to add it.

Now I configured alternatives to use this version of java whenever I call java from command line:

/usr/sbin/alternatives –config java
I selected 2 as my default here in the options screen which came up.

Note: I can at any time run this command again and choose a different version of Java as my default by just selecting a different number.

Similarly I configured other java executables using alternatives:
/usr/sbin/alternatives –install /usr/bin/javac javac /usr/java/latest/bin/javac 1
/usr/sbin/alternatives –install /usr/bin/jconsole jconsole /usr/java/latest/bin/jconsole 1
/usr/sbin/alternatives –install /usr/bin/jvisualvm jvisualvm /usr/java/latest/bin/jvisualvm 1
/usr/sbin/alternatives –install /usr/bin/jps jps /usr/java/latest/bin/jps 1
/usr/sbin/alternatives –install /usr/bin/jar jar /usr/java/latest/bin/jar 1
/usr/sbin/alternatives –install /usr/bin/jstack jstack /usr/java/latest/bin/jstack 1

Note: WordPress in its infinite wisdom concates two - to create a separate long hypen character, replace that with two - (hypen) in the commands above and elsewhere in this article.

Note: You don’t need to run config if there is only one application associated with a name as is in the above cases.

So now you know how to easily configure Java or any other application on Linux, applications which may have multiple alternatives or versions like say text editor or browser.

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :