openSuSE 下面配置 java环境变量

来源:互联网 发布:天书世界机甲进阶数据 编辑:程序博客网 时间:2024/05/16 09:19

本文已安装jdk 5.0为例,讲解openSuSE10.2下的jdklinux下的安装过程和linux下环境变量的设置,虽然本文的示范环境为openSuSE10.2,但是在其他的linux操作系统下依然有意义。

一、安装JDK

sun网站上直接下载JDK.

键入http://java.sun.com/javase/downloads/index_jdk5.jsp

从中选择你要安装的jdk类型,jdk5.0的发布形式为两种:Self-extracting Binary File RPM Packages,下面是对这两种安装包的说明:

  • Self-extracting Binary File - This file can be used to install the JDK in a location chosen by the user. This one can be installed by anyone (not only root users), and it can easily be installed in any location. As long as you are not root user, it cannot displace the system version of the Java platform suppled by Linux. To use this file, see Installation of Self-Extracting Binary below.

  • RPM Packages - A rpm.bin file containing RPM packages, installed with the rpm utility. Requires root access to install, and installs by default in a location that replaces the system version of the Java platform supplied by Linux. To use this bundle, see Installation of RPM File below.

选择最适合你的安装形式:

Note: For any text on this page containing the following notation, you must substitute the appropriate JDK update version number for the notation.

<version>
For example, if you are downloading update 1.5.0_01, the following command:
./jdk-1_5_0_<version>-linux-i586.bin
would become:
./jdk-1_5_0_01-linux-i586.bin


安装 Self-Extracting Binary

Use these instructions if you want to use the self-extracting binary file to install the JDK. If you want to install RPM packages instead, see Installation of RPM File.

1. Download and check the download file size to ensure that you have downloaded the full, uncorrupted software bundle.

You can download to any directory you choose; it does not have to be the directory where you want to install the JDK.
Before you download the file, notice its byte size provided on the download page on the web site. Once the download has completed, compare that file size to the size of the downloaded file to make sure they are equal.

2. Make sure that execute permissions are set on the self-extracting binary.

Run this command:
chmod +x jdk-1_5_0_<version>-linux-i586.bin

3. Change directory to the location where you would like the files to be installed.

The next step installs the JDK into the current directory.

4. Run the self-extracting binary.

Execute the downloaded file, prepended by the path to it. For example, if the file is in the current directory, prepend it with "./" (necessary if "." is not in the PATH environment variable):
./jdk-1_5_0_<version>-linux-i586.bin
The binary code license is displayed, and you are prompted to agree to its terms.
The JDK files are installed in a directory called jdk1.5.0_<version> in the current directory. Follow this link to see its directory structure. The JDK documentation is a separate download.
Note about Root Access: Unbundling the software automatically creates a directory called jdk1.5.0_<version>. Note that if you choose to install the JDK into system-wide location such as /usr/local, you must first become root to gain the necessary permissions. If you do not have root access, simply install the JDK into your home directory, or a subdirectory that you have permission to write to.
Note about Overwriting Files: If you unpack the software in a directory that contains a subdirectory named jdk1.5.0_<version>, the new software overwrites files of the same name in that jdk1.5.0_<version> directory. Please be careful to rename the old directory if it contains files you would like to keep.
Note about System Preferences: By default, the installation script configures the system such that the backing store for system preferences is created inside the JDK's installation directory. If the JDK is installed on a network-mounted drive, it and the system preferences can be exported for sharing with Java runtime environments on other machines. As an alternative, root users can use the -localinstall option when running the installation script, as in this example:
jdk-1_5_0_<version>-linux-i586.bin -localinstall
This option causes the system preferences to be stored in the /etc directory from which they can be shared only by VMs running on the local machine. You must be root user for the -localinstall option to work.
See the Preferences API documentation for more information about preferences in the Java platform.

安装 RPM File

Use these instructions if you want to install JDK in the form of RPM packages. If you want to use the self-extracting binary file instead, see Installation of Self-Extracting Binary.

1. Download and check the file size.

You can download to any directory you choose.
Before you download the file, notice its byte size provided on the download page on the web site. Once the download has completed, compare that file size to the size of the downloaded file to make sure they are equal.

2. Become root by running the su command and entering the super-user password.

3. Extract and install the contents of the downloaded file.

Change directory to where the downloaded file is located and run these commands to first set the executable permissions and then run the binary to extract and run the RPM file:
chmod a+x jdk-1_5_0_<version>-linux-i586-rpm.bin  ./jdk-1_5_0_<version>-linux-i586-rpm.bin
Note that the initial "./" is required if you do not have "." in your PATH environment variable.
The script displays a binary license agreement, which you are asked to agree to before installation can proceed. Once you have agreed to the license, the install script creates and runs the file jdk-1_5_0_<version>-linux-i586.rpm in the current directory.
NOTE - If instead you want to only extract the RPM file but not install it, you can run the .bin file with the -x argument. You do not need to be root to do this.

4. Delete the bin and rpm file if you want to save disk space.

5. Exit the root shell.



二、配置环境变量

1.
修改用户环境变量

假设使用java的用户为 test(通常就是你自己的),编辑该用户的.bashrc文件进行环境变量设置。

修改用户环境变量,只对本用户起作用,不影响其它用户
# vi /home/test/.bashrc

.bashrc文件中加入:


export JAVA_HOME=/usr/java/jdk1.5.0_12
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=$CLASSPATH:.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib




退出shell,重新登陆,环境生效



2.
修改全局的环境变量
当然也可以通过更改/etc/profile来实现,不过不推荐这么做,因为这样的设置将对所以用户的shell都生效,对系统安全会产生影响。就是在这个文件的最后加上:

# vi /etc/profile

在该文件中加入:

export JAVA_HOME=/usr/java/jdk1.5.0_12
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=$CLASSPATH:.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib


一定要在CLASSPATH路径中加入$CLASSPATH:

在终端下执行 source /etc/profile 使配置生效




3.
可以用以下命令查看

显示JAVA版本  java -vesion

如果环境变量设置生效的话,这个命令所显示的java版本,就是我们安装jdk包的版本哦



测试javac是否有效 ,键入 javac 在终端控制台 如果有下面输出,证明安装正确。

用法:javac <选项> <源文件>

其中,可能的选项包括:

-g 生成所有调试信息

-g:none 不生成任何调试信息

-g:{lines,vars,source} 只生成某些调试信息

-nowarn 不生成任何警告

-verbose 输出有关编译器正在执行的操作的消息

-deprecation 输出使用已过时的 API 的源位置

-classpath <路径> 指定查找用户类文件的位置

-cp <路径> 指定查找用户类文件的位置

-sourcepath <路径> 指定查找输入源文件的位置

-bootclasspath <路径> 覆盖引导类文件的位置

-extdirs <目录> 覆盖安装的扩展目录的位置

-endorseddirs <目录> 覆盖签名的标准路径的位置

-d <目录> 指定存放生成的类文件的位置

-encoding <编码> 指定源文件使用的字符编码

-source <版本> 提供与指定版本的源兼容性

-target <版本> 生成特定 VM 版本的类文件

-version 版本信息

-help 输出标准选项的提要

-X 输出非标准选项的提要

-J<标志> 直接将 <标志> 传递给运行时系统