I work as java/j2ee application developer so kindly tell me how to set JAVA_HOME, JRE_HOME, CLASSPATH so that I may use Tomcat, Mysql ,java all together in Terminal.

I may be using all together in a pure CLI server environment, or in a GUI desktop environment.


4 Answers 4


Using 12.04, placing the information in /etc/environment worked for me:

tku@buster:~$ cat /etc/environment 

You wanna make sure your Java is the default:

Set Default Java Version

Use the alternatives command to set the default java version.

~ ~↓↓$↓↓ sudo alternatives --config java

Select Java:

If your system has multiple Java versions, the above command will list all Java versions.

There are 3 programs which provide 'java'.

  Selection    Command
 + 1           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-
   2           java-latest-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-
*  3           /usr/java/jdk-17.0.1/bin/java

Enter to keep the current selection[+], or type selection number: whateverisyours

Verify Java Version

Check the java version using the following command.

~ ~↓↓$↓↓ java --version

output feks.:

java 18.0.2 2022-07-19
Java(TM) SE Runtime Environment (build 18.0.2+9-61)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.2+9-61, mixed mode, sharing)

Then, add the required variables.

To set the environment variables for a particular user, place the below variables in the .bash_profile file in the home directory or in the /etc/environment file for system wide settings.

~ ~↓↓$↓↓ sudo nano /etc/environment

and your information there is current version of Java:

export PATH=$PATH:/usr/java/jdk-18.0.2/bin/
export JAVA_HOME=/usr/java/jdk-18.0.2/  

if your using JRE then it will look something like this:

export PATH=$PATH:/usr/java/jre*****/bin/
export JAVA_HOME:/usr/java/jre*****/

Test it by executing:

~ ~↓↓$↓↓ echo $JAVA_HOME
~ ~↓↓$↓↓ echo $PATH
~ ~↓↓$↓↓ java --version

If you have multiple versions of Java (or, for that matter, multiple versions of anything) installed, and want to be able to switch between them, Environment Modules is worth considering. It enables you to load, unload, and switch between the environment setups for different installed versions of any software that it's configured to handle. It's a de facto standard on clusters and, although it's a bit of a pain to set up at first, it's well worth the effort, IMHO.



sudo nano /etc/environment

Add the path to the "bin" directory inside the java directory after the colon.

PATH = "blah/blah/blah:........../xyz/yvc:/YOUR/PATH/TO/JAVA/BIN"

Save it. Now run the following command...

source /etc/environment

Check whether it is properly installed.

java -version

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .