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. Effective C++ 条款27

    尽量少做转型动作 尽量少做转型动作有什么目的?非常明显无非就是提高程序的稳定性.提高程序的运行效率. 那么.有哪些转型方式?每种方式都有什么弱点? 这是我们本节学习的重点. C++有四种转型: con ...

  2. Python27中Json对中文的处理

    应用场景如下:从api下载数据,json解析,存入字典,定期保存.重启程序需要加载保存的文本. 问题1:json中都是unicode串,存到文本里都是些\u*** 解决:关闭ensure_ascii开 ...

  3. php平滑升级

    php平滑升级 1.既然是平滑升级,是不需要宕机,也不需要停止服务,是不存在关闭网站的说法 2.而升级最后的重启,重启的是PHP加载的配置文件,不影响你当前运行的网站 3.php-fpm是属于Fast ...

  4. 使用socket BPF/Linux内核工程导论——网络:Filter(LSF、BPF、eBPF)

    使用socket BPF linux 下的 包过滤器 BPF Linux内核工程导论——网络:Filter(LSF.BPF.eBPF) 注意(文中描述的内容): 此外,这段BPF代码还存在的一个问题是 ...

  5. 用BeanUtilsDate类型值为空报错的解决方法

    除BeanUtils外还有一个名为PropertyUtils的工具类,它也提供copyProperties()方法,作用与BeanUtils的同名方法十分相似,主要的区别在于后者提供类型转换功能,即发 ...

  6. 【FAI】七日年化收益与万份收益的理解

    七日年化收益:其实指的一年的收益(应该忽略”七日”),这里的七日指的是取最近七日年化的结果 万份收益:每万元每天的收益 可以使用下图来清晰识别: 例子: 10000元按照5%的七日年化收益计算的话: ...

  7. java beanUtils框架

    beanUtils是Apache觉得sun公司的内省不够爽,自己又开发了一套可以操作JavaBean的API 所以beanUtils是第三方jar包,使用beanUtils要导包: 在工程目录下新建一 ...

  8. 防止线程退出的几种方案-不再while(true)

    有时候 调试程序的时候 .我们需要防止程序退出.比如调试一个定时服务. 方法1 while(true) {Thread.Sleep(1000)} 方法 2——(推荐) Well when you do ...

  9. java.lang.NoClassDefFoundError: Could not initialize class com.demo.jdbc.utils.MyJdbcUtils

    jdbc编写工具类的过程中测试失败, 出现如下错误:   原因:初始化的过程中执行静态代码块的过程中出现错误, 也就是说, 加载配置文件错误: 没有加载到指定路径的配置文件. 我的MyJdbcUtil ...

  10. nyoj------擅长排列的小明

    擅长排列的小明 时间限制:1000 ms  |           内存限制:65535 KB 难度:4   描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序 ...