How to set JAVA environment variables JAVA_HOME and PATH in Linux

After installing new java (jdk or jre) or latest Java you may have usually find that the version of java is not exactly the same which you have installed. It might be showing you the same old version.

How to check the java version which is currently set in linux system ?
Answer: Run the below given command

# java --version

or 

# java -version

Now we will install new Java and set the java variables

Download the rpm file from Oracle website. Click here(If you have installed with tar ball then again there is no problem)

I installed the jdk-1.7.0_21-fcs.i586 hence showing practical case to case (You can skip the step if you installed through tar ball)

Install JDK (java) rpm

rpm -ivh jdk-1.7.0_21-fcs.i586

Generally after installation Java file goes to the path /usr/java/jdk-xx-version/. In my case it is in

/usr/java/jdk1.7.0_21

To check where is the latest Java (JDK or JRE) you have installed in your system. Run below given command

find / -name java

How to set Java variable environment

Follow the given below steps (Replace the version no. as per your new Java version installed in your system)

Step1 : Open /root/.bash_profile through your text editor. (I prefer to use vi editor)
And paste the given below two lines

export JAVA_HOME=/usr/java/jdk1.7.0_21
export PATH=/usr/java/jdk1.7.0_21/bin:$PATH

Step 2 : Now enable the Java variable without system restart (On system restart it bydefault set the java variable)

 source /root/.bash_profile

Step 3: Now check the Java version,JAVA_HOME and PATH variables.It should show you correct information as you have set.

java --version

echo $JAVA_HOME

echo $PATH

Given below is the reference of my system’s root bash_profile file

[root@localhost ~]# cat /root/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

export JAVA_HOME=/usr/java/jdk1.7.0_21
export PATH=/usr/java/jdk1.7.0_21/bin:$PATH
[root@localhost ~]#

Other files and location where you can set Java variable and what are the difference

(1) /etc/profile = To set environment variable to all users

(2) $HOME/.bashrc = To set environment for login user.
(3) $HOME/.bash_profile = To set environment for login user

Note: .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

(4) Create a shell script inside /etc/profile.d/ with .sh extension. and make the file executable.

(5) Create a shell script in some other location and give its path in /etc/rc.local

Referred : http://sharadchhetri.com/2013/06/03/how-to-set-java-environment-variables-in-linux-or-centos/

How to set JAVA environment variables in Linux or CentOS的更多相关文章

  1. Java environment variables and their functionality

    Explanations of Functionalities: 1. PATH env variable It is used to search the command directory whe ...

  2. Linux environment variables (环境变量)

    Environment variables are often used to store a list of paths of where to search for executables, li ...

  3. CVE: 2014-6271、CVE: 2014-7169 Bash Specially-crafted Environment Variables Code Injection Vulnerability Analysis

    目录 . 漏洞的起因 . 漏洞原理分析 . 漏洞的影响范围 . 漏洞的利用场景 . 漏洞的POC.测试方法 . 漏洞的修复Patch情况 . 如何避免此类漏洞继续出现 1. 漏洞的起因 为了理解这个漏 ...

  4. Debian Environment Variables

    原文:EnvironmentVariables General Environment variables are named strings available to all application ...

  5. How to install JDK (Java Development Kit) on Linux

    This tutorial will guide you on how to install JDK (Java Development Kit) on Linux. Since I use Cent ...

  6. Java学习笔记之Linux下的Java安装和配置

    0x00 概述 由于使用 yum 或者 apt-get 命令 安装 openjdk 可能存在类库不全,从而导致用户在安装后运行相关工具时可能报错的问题,所以此处我们推荐采用手动解压安装的方式来安装 J ...

  7. Java Environment Setting

    As a non-Java developer, I am quit stuck in Java environment setting because I am not familiar with ...

  8. Java学习心得之 Linux下搭建Java环境

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Java学习心得之 Linux下搭建Java环境 1.前言2.JDK安装3.配置环境变量4. ...

  9. Java学习心得之 Linux下搭建JavaWeb环境

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Java学习心得之 Linux下搭建JavaWeb环境 1. 前言2. Java安装3. t ...

随机推荐

  1. matlab中find函数的使用说明

    matlab中如何统计一个矩阵M中零的个数 size(find(M==0),1) 原文:http://blog.sina.com.cn/s/blog_707b64550100rbh3.html fin ...

  2. UE如何去除重复行,删除重复行

    1 如图所示,原理一个文本文档有两千多行,但是有大量的重复 2 使用UE的排序功能,有删除重复行的选项. 3 处理之后只剩下不到两百行了.

  3. ArcEngine真正释放锁文件,彻底移除图层

    ArcMap在加载图层时会自动生成一个lock格式的加锁文件,右击移除图层后,加锁文件也会自动删除.但AE开发中却不能正常删除,移除图层后加锁文件依然存在,这就导致在其他地方无法对该图层进行操作,只有 ...

  4. Intent跳转到系统应用中的拨号界面、联系人界面、短信界面及其他

    现在开发中的功能需要直接跳转到拨号.联系人.短信界面等等,查找了很多资料,自己整理了一下. 首先,我们先看拨号界面,代码如下: Intent intent =new Intent(); intent. ...

  5. linux下apache的使用

    Linux安装配置apache http://www.cnblogs.com/fly1988happy/archive/2011/12/14/2288064.html 1.获取软件: http://h ...

  6. Java高并发syncronized深入理解

    1.Synchronized的作用: 能够保证在同一时刻最多只有一个线程执行该段代码,以达到保证并发安全的效果. 2.地位: 1)Synchronized是java的关键字,并java的怨言原生支持: ...

  7. jquery-序列化表单

      createTime--2016年9月25日08:54:48参考链接:http://www.w3school.com.cn/tags/html_ref_urlencode.htmljQuery的s ...

  8. linux 和windows系统下同时可用的UML建模工具(umbrello),超强

    原文地址:linux 和windows系统下同时可用的UML建模工具(umbrello),超强 作者:zhangjiakouzf OPEN SOURCE 的 UML建模工具 -- umbrello   ...

  9. CentOS6.3的VNC--远程桌面

    2G内存的服务器开启Gnome图形化界面应该没什么问题.1G还有512M的内存的就不敢开启了,现在内存正常状态就已经60%左右了. CentOS6.3服务器,Gnome图形化界面按照阿里官方步骤:一. ...

  10. Xiuno 开发手册正式发布。

    下载地址:http://bbs.xiuno.com/down/xiuno.chm.tar.gz