Solved

How to set JAVA_HOME in linux

Posted on 2014-01-13
10
574 Views
Last Modified: 2014-01-13
How to set JAVA_HOME  in  Red Hat  Enterprise Linux Server Release 5.5 ?

Which files to edit ?

Please tell me the steps .
0
Comment
Question by:cofactor
  • 5
  • 5
10 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39776060
To set it for all users use /etc/profile, to set it for a particular user use the .bashrc file in the user's HOME directory to add

export JAVA_HOME=/path/to/java/home

Don't forget to logout and re-login.
0
 

Author Comment

by:cofactor
ID: 39776202
I am bit confused .

When I  run this  I get  

# whereis javac
javac: /usr/bin/javac


and ..

# java -version
java version "1.6.0"
OpenJDK  Runtime Environment (build 1.6.0-b09)
OpenJDK Client VM (build 1.6.0-b09, mixed mode)


]# whereis java
java: /usr/bin/java /etc/java /usr/lib/java /usr/share/java /usr/share/man/man1/java.1.gz


Can you please tell what to  I put in  $JAVA_HOME ?
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 400 total points
ID: 39776285
Try

/usr/sbin/alternatives --config java
/usr/sbin/alternatives --config javac

Do you get an reply saying that there are programs providing java/javac?

If you get one program you'll see the path to the respective executable.
If there is no "+" in front type the selection number "1", hit <Enter> and follow the instructions.
If you see several ones you have the option to choose which one to make the default, just like above.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:cofactor
ID: 39776286
I get this ...

/usr/sbin/alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           /usr/lib/jvm/jre-1.4.2-gcj/bin/java
*+ 2           /usr/lib/jvm/jre-1.6.0-openjdk/bin/java




so I guess my  java home would be ...

JAVA_HOME=/usr/lib/jvm/jre-1.6.0-openjdk


right ?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39776294
So you have two java versions installed, and 1.6 is set as the default (I assume that's what you desire).

Yes, JAVA_HOME should be "/usr/lib/jvm/jre-1.6.0-openjdk" in this case.

You could type "2" and hit <Enter> and follow the instructions to set 1.6 as the default again. Maybe this will set JAVA_HOME, but if it doesn't see my first comment where to add it.

Best use /etc/profile, so all users will have the correct version as the default.
You should also check your PATH variable. Does it contain $JAVA_HOME/bin (the resolved version, that is, e.g. "/usr/lib/jvm/jre-1.6.0-openjdk/bin")? If it doesn't set it accordingly ir /etc/profile, too.
0
 

Author Comment

by:cofactor
ID: 39776308
I  have set this  now in  /etc/profile


JAVA_HOME="/usr/lib/jvm/jre-1.6.0-openjdk";
export JAVA_HOME;
#PATH=$JAVA_HOME/bin:$PATH;
#export PATH;
ANT_HOME="/opt/apache-ant-1.8.2";
export ANT_HOME;
PATH=$JAVA_HOME/bin:$PATH:$ANT_HOME/bin:$CATALINA_HOME/bin;
export CATALINA_HOME="/home/softwares/apache-tomcat-6.0.32";
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39776334
Since the PATH contains a reference to CATALINA_HOME you must set this variable before setting PATH (interchange the last two lines):

JAVA_HOME="/usr/lib/jvm/jre-1.6.0-openjdk";
export JAVA_HOME;
ANT_HOME="/opt/apache-ant-1.8.2";
export ANT_HOME;
CATALINA_HOME="/home/softwares/apache-tomcat-6.0.32";
export CATALINA_HOME
PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$CATALINA_HOME/bin:$PATH

For consistency use the same format of "export" in all cases. Because you used the format

VAR=value
export VAR

for JAVA_HOME and ANT_HOME I'd suggest using it for CATALINA_HOME as well (I admit, it's a matter of taste).

Your standard PATH should come after the JAVA/CATALINA/ANT paths, to avoid possible confusion.

Once these modifications are applied everything should be just fine. Don't forget to logout and re-login to make the changes effective for your shell session.
0
 

Author Comment

by:cofactor
ID: 39776351
This is working fine.   Thank you very much.

I have a small query  here .

What happens when we do export VAR ?
 Is not just  VAR=value  is enough to use  the variable later ?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39776368
"export" adds the specified variable to the environment, which means that it is made available to subprocesses of the calling shell.

Without "export" a variable is only available to your current session but not to scripts/programs called from that session.
0
 

Author Closing Comment

by:cofactor
ID: 39776524
Excellent
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tomcat administrtor 12 48
tomcat startup error 5 65
windows 7 starter missing password 21 78
Java: The Public Class Main 4 17
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question