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. (转)U3D不同平台载入XML文件的方法——IOS MAC Android

    自:http://www.cnblogs.com/sifenkesi/archive/2012/03/12/2391330.html 在PC上和IOS上读取XML文件的方式略有差别,经测试,IOS上不 ...

  2. [Node.js]32. Level 7: Working with Lists -- Redis

    As we saw in the video, redis can do more than just simple key-value pairs. We are going to be using ...

  3. Pascal's Triangle I,II

    题目来自于Leetcode https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numR ...

  4. 【statistics】理想论坛2018-4-25日统计

    说明:利用理想论坛爬虫1.07版(http://www.cnblogs.com/xiandedanteng/p/8954115.html) 下载了前十页主贴及子贴,共得到359619条数据,以此数据为 ...

  5. Java从零开始学十三(封装)

    一.什么是封装,为什么要封装 对面向对象而言:封装就是将方法和属性包装到一个程序单元中,并且这个单元以类的形式实现. 简单讲:封闭就是将属性私有化,提供公有方法来访问私有属性 封装的作用: 封装反映和 ...

  6. 子查询三(在FROM子句中使用子查询)

    FROM子句中使用子查询一般都是返回多行多列,可以将其当作一张数据表 示例一.查询出每个部门的编号,名称,位置,部门人数,平均工资 SELECT d.deptno,d.dname,d.loc,temp ...

  7. 算法笔记_187:历届试题 网络寻路(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 X 国的一个网络使用若干条线路连接若干个节点.节点间的通信是双向的.某重要数据包,为了安全起见,必须恰好被转发两次到达目的地.该包可能在任 ...

  8. 安装itunes时提示不能删除旧版本的apple应用程序支持

    下载我 选择需要卸载清理的程序(所有与Apple有关的) ok

  9. shell脚本中的几个括号总结(小括号/大括号/花括号)

    from:http://www.cnblogs.com/hanyan225/archive/2011/10/06/2199652.html 是毋庸置疑的,方便了我们也迷惑了我们,比如这些杂七杂八的括号 ...

  10. from会存在潜在的陷阱

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #from会存在潜在的陷阱 #from时,可能会遇到相同变量名,变量会被悄悄覆盖掉, #但是import语句不 ...