`
as619864232
  • 浏览: 325834 次
社区版块
存档分类
最新评论

在 Linux 下安装 JDK 和设置 JAVA 环境变量

阅读更多

 

I know lot of you starting out new as Linux users or Java learners on Linux platform find it an issue to install JDK or may be configure it to start using after installing. At time the version of java that comes with the package manager (apt-get for Ubuntu) would be an older version and you would require you to download the compressed binaries. In any case I have outlined how to configure in both the cases.

Case: 1- You would have to download the required JDK binary package from here. And follow the Steps for Case-1 below. This would involve extracting the compressed binary and then setting up the $PATH.

Case: 2- You have installed the JDK package using the apt-get and it installs JDK to some location (I haven’t used this approach because when ever I tried I ended up with an older version of JDK, so I have not explained it in detail).

Also note that usually the Linux installation might come with the OpenJDK but I have never used that before.

Steps for Case-1

Suppose you happen to download the JDK from the Oracle Downloads site here, then you can follow the below procedure:

1. Download the required Java SE package from the Oracle Download site here.  [Download the compressed Binary and Preferably Java SE 7]

2. Extract it in to a location of you choice- I used the /home/jdk<version>

3. Update the $PATH variable to locate the jdk/bin directory so that the shell can recognize the java and javaccommands. This can be done by appending the PATH variable with the location to the jdk bin. In bash you can either use a seperate alias file- .bash_aliases and refer it in the .bashrc file or directly add in the .bashrc file. Here I am using a seperate alias file: .bash_aliases - In this file u can specify the variable values- for PATH, CLASSPATH and others required ones and also aliases for you various commands.

There’s a catch here: If you just initialize the $PATH variable with the jdk bin location then you will loose out the access to other bin dirs like- /usr/bin, /bin, /usr/local/bin and so on. Instead you would have to update/append the $PATH variable.

Lets assume you extracted the JDK compressed binary to- /home/jdk1.6.0_16

(Note: the dir- jdk1.6.0_16 naming would differ based on the version of JDK you have downloaded)

So the .bash_aliases file would look something like this

JAVA_HOME=/home/usr/jdk1.6.0_16
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin:
export PATH
CLASSPATH=$JAVA_HOME/lib/:.
export CLASSPATH

The ‘:‘ is the separator, for windows it would be “;“. Its optional to update the CLASSPATH as one can provide it during the execution of Java program or if you are using an IDE it will manage the CLASSPATH or for that matter if you are using an tools like Ant, Maven. So just in case you are used to providing the CLASSPATH and may be use command line at times for you demo programs here’s a small tip:

Note that in the CLASSPATH- there’s an addition value “.“. This points to the current directory and will be helpful when the class files are in the current directory. Otherwise java will search for the classes in the directories mentioned in the CLASSPATH and will return ClassNotFoundException if the current directory is not part of the CLASSPATH and the required class files are in the current directory. Other option is to use -cpand specify the classpath. In that case the value of the CLASSPATH variable is overridden with the value specified by -cp.

Steps for Case-2

Suppose you have use the apt-get command to install the JDK, the you can follow the below procedure and this has to be done every time you enter the terminal or best is to put it in a .bashrc or .bash_aliases file as mentioned above:

JAVA_HOME=/usr/lib/jvm/java
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH

 

分享到:
评论

相关推荐

    linux快速安装jdk1.8方式及环境变量配置

    linux快速安装jdk1.8方式及环境配置 一、下载jdk jdk下载地址:...– 安装(将下载的压缩文件解压然后配置环境变量) 进入linux创建Java文件mkdir /usr/ja

    linux一键安装jdk 1.8 并且 配置环境变量 shell 脚本

    linux一键安装jdk 1.8 并且 配置环境变量 shell 脚本

    Linux配置JDK环境变量

    ### Linux配置JDK环境变量详解 #### 一、下载与准备JDK 1. **下载JDK**:首先,从Sun Microsystems(现已...通过以上步骤,您可以成功地在Linux系统上安装和配置JDK环境变量,为开发Java应用程序提供必要的环境支持。

    linux下载安装jdk配置环境变量

    Linux 操作系统下的 JDK 安装和配置是一个非常重要的步骤,特别是在开发 Java 应用程序时。下面我们将详细介绍如何下载、安装和配置 JDK 在 Linux 操作系统下。 一、下载 JDK 安装包 首先,我们需要下载 JDK 的...

    java环境变量配置 JDK环境变量

    ### Java环境变量配置详解 ...通过上述步骤,我们可以有效地配置好Java环境变量,从而在开发过程中更加便捷地使用Java相关的工具和命令。此外,理解每个环境变量的具体含义及其作用对于后续深入学习Java开发非常重要。

    linux下jdk安装配置

    Linux 系统上安装 JDK 是 Java 开发人员的基本技能之一,本文将指导学习人员初步学会在 Linux 系统上安装 JDK,并进行环境变量的配置,避免安装时犯下一些常见的错误。 一、Java 运行硬件环境 在 Linux 系统上安装...

    linux下的配置jdk的环境变量

    为了在 Linux 下配置 JDK 环境变量,需要设置环境变量 PATH、CLASSPATH 和 JAVA_HOME。这些变量的设置将影响 JDK 的安装和配置。 环境变量 PATH 的设置是为了使得 Linux 系统可以找到 JDK 的可执行文件,例如 javac...

    在linux下安装JDK

    在安装 JDK 之前,需要确保 Linux 系统已经安装了必要的依赖项和工具。通常情况下,Linux 系统已经安装了基本的工具,如 tar、vi 等。同时,需要确保系统有足够的磁盘空间来存储 JDK。 二、下载和解压 JDK 首先,...

    Linux下配置java环境变量

    在Linux系统中配置Java环境变量是一项基础且关键的IT技能,尤其对于开发人员而言,确保Java运行环境正确无误是进行Java编程的前提条件。本文将深入解析如何在Linux环境下配置Java(JDK)的环境变量,包括理解环境...

    linux下jdk安装、环境变量以及软连接设置.doc

    这篇文档主要介绍了在CentOS操作系统上安装Oracle JDK 1.6.0_29的步骤,以及如何配置相应的环境变量和创建软连接。以下是详细的操作过程: 1. **下载JDK**: Oracle JDK通常不会被默认包含在Linux发行版如CentOS中...

    suse系统下安装JDK

    在安装JDK之前,需要检查是否已经安装了自带的JDK 1.4。可以使用命令`#rpm -qa | grep gcj`来检查。如果输出为空,说明没有安装JDK 1.4。如果输出有内容,需要卸载已经安装的JDK 1.4。卸载命令为`#rpm -e --nodeps ...

    linux下安装JDK

    在安装JDK之前,我们需要先查看Linux系统中是否已经安装了JDK。我们可以使用以下命令来查看已经安装的JDK: ``` # rpm -qa|grep jdk ``` 这条命令将显示已经安装的JDK的信息。如果已经安装了JDK,我们可以卸载它,...

    Linux下的JDK包和环境变量配置

    版本:JDK8、JDK11、JDK17 包含了多版本java共存方案以及版本切换方案

    linux 下面安装jdk

    本文将详细介绍如何在Linux环境下安装JDK,以及设置环境变量的步骤。 首先,让我们从下载JDK开始。在提供的示例中,我们看到的是通过RPM包安装JDK6的过程。通常,你可以从Oracle官网或其他可靠的源下载适合你Linux...

    linux下安装jdk及配置环境变量及apache-tomact的安装

    在Linux环境下,安装Java Development Kit (JDK) 和Apache Tomcat是进行Web应用程序开发和部署的基础。下面将详细讲解这两个组件的安装过程。 首先,我们来看JDK的安装步骤: 1. **下载JDK**: 从SUN的官方网站...

    JDK的安装和配置环境变量

    如果显示出版本信息,那么恭喜你,JDK安装和环境变量配置成功。 理解并熟练掌握JDK的安装与环境变量配置,是每个Java开发者的基础技能。这不仅关乎能否运行Java程序,也是进行更深入的Java开发所必需的。通过反复...

    Linux下java环境变量配置脚本

    Linux系统下的jdk解压及配置环境脚本 具体步骤: 1.使用chmod +x jdk.sh赋予可执行权限 2.确保在root环境下运行该脚本 3.将该脚本与*.tar.gz放于同一文件夹 4.输入文件名称时请不要输入.tar.gz

    Linux下安装jdk1.8并配置环境变量的教程

    ### Linux下安装JDK 1.8并配置环境变量的详细教程 在本文中,我们将详细介绍如何在Linux环境下安装JDK 1.8并正确配置其环境变量。这对于那些希望在Linux平台上进行Java开发的人来说非常有用。 #### 一、准备工作 ...

    Linux安装JDK并配置环境变量

    在 Linux 系统中安装 JDK 并配置环境变量是一个非常重要的步骤,因为 JDK 是 Java 语言的核心组件,许多 Java 应用程序都需要它来编译和运行。下面我们将详细介绍如何在 Linux 系统中安装 JDK 并配置环境变量。 第...

Global site tag (gtag.js) - Google Analytics