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. SharePoint 2013网站突然不能登录了。

    SharePoint 2013网站突然不能登录了,访问的时候,总是报错: The list has not shared with you.   原因: 原来我不知道什么时候把web applicat ...

  2. MySQL的IFNULL函数

    MySQL函数里有一个很有用的函数IFNULL,它的形式是IFNULL(fieldA,fieldB),意义是当字段fieldA是NULL时取fieldB,不是NULL时取fieldA的值. 这个函数与 ...

  3. linux查询文件中某几行

    查询文件中某几行: sudo cat /etc/tinyproxy.conf | head -n | tail -n + [一]从第3000行开始,显示1000行.即显示3000~3999行 cat ...

  4. 解决 在POM配置Maven plugin提示错误“Plugin execution not covered by lifecycle configuration”

    eclipse在其POM文件的一处提示出错如下: Plugin execution not covered by lifecycle configuration: org.apache.maven.p ...

  5. openerp 中文乱码及界面翻译不全

    openerp中文报表乱码问题,即是字体问题,环境是ubuntu 12.04 openerp 6.1, deb all in one安装方式 一.cd usr/share/fonts/truetype ...

  6. Fail Fast and Fail Safe Iterators in Java

    https://www.geeksforgeeks.org/fail-fast-fail-safe-iterators-java/ Fail Fast and Fail Safe Iterators ...

  7. 【Linux】数组与关联数组

    数组 数组的定义: variable=(arg1 arg2 arg3 -) 中间用空格分开,数组的下标从0开始 1.获取下标为n的元素 variable[n] 不存在数组溢出的情况,如果下标n> ...

  8. 科学计算法帮助类MathUtils

    import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; /** * 科学计算 ...

  9. C#:定义窗口快捷键

    事情的关键是要设置Form的KeyPreview属性,然后再在KeyDown事件中检查按键. public class TEST : Form { public TEST() { Initialize ...

  10. login.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...