Skip to content Skip to main navigation Skip to footer

JAVA

CentOS 使用yum工具安装JAVA SDK

如何在centos系统里安装JAVA JDK 运行时环境呢?本文将会介绍通过使用yum命令安装JAVA SDK 开发环境。
CentOS 6.x 和5.x 版本默认都安装了openJDK运行时环境。这是一个开源的java版本。
java
CentOS 默认安装的JAVA 软件包包括:
[cc lang=”php”] java-1.7.0-openjdk – OpenJDK 运行时环境
java-1.7.0-openjdk-devel – OpenJDK 开发环境
[/code] [cc lang=”php”] 使用下面的命令查看yum软件库里可用的JDK 软件包:
yum search java | grep -i –color JDK
[/code] 命令输出:
[cc lang=”php”] [[email protected] Desktop]# yum search java | grep -i –color JDK
ldapjdk-javadoc.x86_64 : Javadoc for ldapjdk
icedtea-web.x86_64 : Additional Java components for OpenJDK – Java browser
java-1.6.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.6.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.6.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.6.0-openjdk-javadoc.x86_64 : OpenJDK API Documentation
java-1.6.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.8.0-openjdk-headless.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.8.0-openjdk-src.x86_64 : OpenJDK Source Bundle
ldapjdk.x86_64 : The Mozilla LDAP Java SDK
[/code] CentOS 安装 Java SDK 开发包
在命令行下通过yum命令安装JAVA SDK:
[cc lang=”php”] yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel
[/code] 命令输出:
[cc lang=”php”] [[email protected] Desktop]# yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.btte.net
* updates: mirrors.btte.net
Setting up Install Process
Resolving Dependencies
–> Running transaction check
—> Package java-1.7.0-openjdk.x86_64 1:1.7.0.71-2.5.3.1.el6 will be installed
–> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: 1:java-1.7.0-openjdk-1.7.0.71-2.5.3.1.el6.x86_64
—> Package java-1.7.0-openjdk-devel.x86_64 1:1.7.0.71-2.5.3.1.el6 will be installed
–> Running transaction check
—> Package libjpeg.x86_64 0:6b-46.el6 will be obsoleted
—> Package libjpeg-turbo.x86_64 0:1.2.1-3.el6_5 will be obsoleting
–> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository
Size
================================================================================
Installing:
java-1.7.0-openjdk x86_64 1:1.7.0.71-2.5.3.1.el6 updates 26 M
java-1.7.0-openjdk-devel x86_64 1:1.7.0.71-2.5.3.1.el6 updates 9.4 M
libjpeg-turbo x86_64 1.2.1-3.el6_5 base 174 k
replacing libjpeg.x86_64 6b-46.el6
Transaction Summary
================================================================================
Install 3 Package(s)
Total download size: 35 M
Is this ok [y/N]:
Is this ok [y/N]: y
Downloading Packages:
(1/3): java-1.7.0-openjdk-1.7.0.71-2.5.3.1.el6.x86_64.rp | 26 MB 00:16
(2/3): java-1.7.0-openjdk-devel-1.7.0.71-2.5.3.1.el6.x86 | 9.4 MB 00:06
(3/3): libjpeg-turbo-1.2.1-3.el6_5.x86_64.rpm | 174 kB 00:00
——————————————————————————–
Total 1.6 MB/s | 35 MB 00:22
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
Userid : CentOS-6 Key (CentOS 6 Official Signing Key)
Package: centos-release-6-3.el6.centos.9.x86_64 (@anaconda-CentOS-201207061011.x86_64/6.3)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : libjpeg-turbo-1.2.1-3.el6_5.x86_64 1/4
Installing : 1:java-1.7.0-openjdk-1.7.0.71-2.5.3.1.el6.x86_64 2/4
Installing : 1:java-1.7.0-openjdk-devel-1.7.0.71-2.5.3.1.el6.x86_64 3/4
Erasing : libjpeg-6b-46.el6.x86_64 4/4
Verifying : 1:java-1.7.0-openjdk-1.7.0.71-2.5.3.1.el6.x86_64 1/4
Verifying : libjpeg-turbo-1.2.1-3.el6_5.x86_64 2/4
Verifying : 1:java-1.7.0-openjdk-devel-1.7.0.71-2.5.3.1.el6.x86_64 3/4
Verifying : libjpeg-6b-46.el6.x86_64 4/4
Installed:
java-1.7.0-openjdk.x86_64 1:1.7.0.71-2.5.3.1.el6
java-1.7.0-openjdk-devel.x86_64 1:1.7.0.71-2.5.3.1.el6
libjpeg-turbo.x86_64 0:1.2.1-3.el6_5
Replaced:
libjpeg.x86_64 0:6b-46.el6
Complete!
[/code] CentOS设置 JAVA_HOME 环境变量
java SDK包默认会安装在“/usr/lib/jvm” 目录下:
[cc lang=”php”] ls -l /usr/lib/jvm
[/code] 命令输出:
[cc lang=”php”] [[email protected] Desktop]# ls -l /usr/lib/jvm
total 8
lrwxrwxrwx. 1 root root 26 Dec 14 04:13 java -> /etc/alternatives/java_sdk
drwxr-xr-x. 3 root root 4096 Nov 26 14:42 java-1.6.0-openjdk-1.6.0.0.x86_64
lrwxrwxrwx. 1 root root 32 Dec 14 04:13 java-1.7.0 -> /etc/alternatives/java_sdk_1.7.0
drwxr-xr-x. 7 root root 4096 Dec 14 04:13 java-1.7.0-openjdk-1.7.0.71.x86_64
lrwxrwxrwx. 1 root root 34 Dec 14 04:13 java-1.7.0-openjdk.x86_64 -> java-1.7.0-openjdk-1.7.0.71.x86_64
lrwxrwxrwx. 1 root root 34 Dec 14 04:13 java-openjdk -> /etc/alternatives/java_sdk_openjdk
lrwxrwxrwx. 1 root root 21 Dec 14 04:12 jre -> /etc/alternatives/jre
lrwxrwxrwx. 1 root root 27 Nov 26 14:42 jre-1.6.0 -> /etc/alternatives/jre_1.6.0
lrwxrwxrwx. 1 root root 37 Nov 26 14:42 jre-1.6.0-openjdk.x86_64 -> java-1.6.0-openjdk-1.6.0.0.x86_64/jre
lrwxrwxrwx. 1 root root 27 Dec 14 04:12 jre-1.7.0 -> /etc/alternatives/jre_1.7.0
lrwxrwxrwx. 1 root root 38 Dec 14 04:12 jre-1.7.0-openjdk.x86_64 -> java-1.7.0-openjdk-1.7.0.71.x86_64/jre
lrwxrwxrwx. 1 root root 29 Dec 14 04:12 jre-openjdk -> /etc/alternatives/jre_openjdk
[/code] 使用export命令设置JAVA_HOME的变量值为包含”bin/java”可执行文件的目录
[cc lang=”php”] export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64
[/code] java环境变量就配置完成了。
下面我们来测试执行一个java程序。
创建一个名为test.java的文件,加入下面的代码:
[cc lang=”java”] public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello, World! osetc.com”);
}
}
[/code] 编译并执行test.java程序:
[cc lang=”php”] javac HelloWorld.java
java HelloWorld
[/code] 程序输出:
[cc lang=”php”] Hello, World!
[/code]

linux下如何配置java7的环境变量

java开发的第一步就是如何来配置java的开发环境,比如如何安装java开发包,如何配置java的环境变量,如何编译java程序。
1.解压安装jdk
在shell终端下进入jdk-6u14-linux-i586.bin文件所在目录,
执行命令 ./jdk-6u14-linux-i586.bin 这时会出现一段协议,连继敲回车,当询问是否同意的时候,输入yes,回车。之后会在当前目录下生成一个jdk1.6.0_14目录,你可以将它复制到 任何一个目录下。
2.需要配置的环境变量
1). PATH环境变量。作用是指定命令搜索路径,在shell下面执行命令时,它会到PATH变量所指定的路径中查找看是否能找到相应的命令程序。我们需要把 jdk安装目录下的bin目录增加到现有的PATH变量中,bin目录中包含经常要用到的可执行文件如javac/java/javadoc等待,设置好 PATH变量后,就可以在任何目录下执行javac/java等工具了。
2).CLASSPATH环境变量。作用是指定类搜索路径,要使用已经编写好的类,前提当然是能够找到它们了,JVM就是通过CLASSPTH来寻找类的。我们 需要把jdk安装目录下的lib子目录中的dt.jar和tools.jar设置到CLASSPATH中,当然,当前目录“.”也必须加入到该变量中。
3).JAVA_HOME环境变量。它指向jdk的安装目录,Eclipse/NetBeans/Tomcat等软件就是通过搜索JAVA_HOME变量来找到并使用安装好的jdk。
3.三种配置环境变量的方法
1).修改/etc/profile文件
如果你的计算机仅仅作为开发使用时推荐使用这种方法,因为所有用户的shell都有权使用这些环境变量,可能会给系统带来安全性问题。
·用文本编辑器打开/etc/profile
·在profile文件末尾加入:

export JAVA_HOME=/usr/share/jdk1.6.0_14
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

·重新登录
·注解
a. 你要将 /usr/share/jdk1.6.0_14改为你的jdk安装目录
b. linux下用冒号“:”来分隔路径
c. $PATH / $CLASSPATH / $JAVA_HOME 是用来引用原来的环境变量的值
在设置环境变量时特别要注意不能把原来的值给覆盖掉了,这是一种
常见的错误。
d. CLASSPATH中当前目录“.”不能丢,把当前目录丢掉也是常见的错误。
e. export是把这三个变量导出为全局变量。
f. 大小写必须严格区分。
2).修改.bash_profile文件
这种方法更为安全,它可以把使用这些环境变量的权限控制到用户级别,如果你需要给某个用户权限使用这些环境变量,你只需要修改其个人用户主目录下的.bash_profile文件就可以了。
·用文本编辑器打开用户目录下的.bash_profile文件
·在.bash_profile文件末尾加入:

export JAVA_HOME=/usr/share/jdk1.6.0_14
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

·重新登录
3).直接在shell下设置变量
不赞成使用这种方法,因为换个shell,你的设置就无效了,因此这种方法仅仅是临时使用,以后要使用的时候又要重新设置,比较麻烦。
只需在shell终端执行下列命令:

export JAVA_HOME=/usr/share/jdk1.6.0_14
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

4.测试jdk
1). 用文本编辑器新建一个Test.java文件,在其中输入以下代码并保存:

public class test {
public static void main(String args[]) {
System.out.println("A new jdk test !");
}
}

2).编译:在shell终端执行命令 javac Test.java
3).运行:在shell终端执行命令 java Test
当shell下出现“A new jdk test !”字样则jdk运行正常。
5.卸载jdk
·找到jdk安装目录的_uninst子目录
·在shell终端执行命令./uninstall.sh即可卸载jdk。

JAVA执行shell命令小工具

在我们的项目中,大部分环境的服务器用的都是Linux,从而使用JAVA与Linux shell通讯,就成了一项很常见的事情。
Ganymed SSH是一个Java实现SSH的项目
Java 实现SSH协议的项目有很多,如JFTP,trilead SSH,JSCH,ganymed SSH等
下面我们主要说的是关于ganymed SSH的一些小使用。
Ganymed SSH-2 for Java是用纯Java实现SSH-2协议的一个项目。可以通过它直接在Java程序中连接SSH服务器,实现基于SSH协议的服务访问。 如远程命令执行和shell访问,本地和远程端口转发,本地数据流转发,X11转发,SCP,SFTP等功能。
散仙测试的demo如下:

package com.qin.shell;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.mortbay.log.Log;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class ExecShell{
  private String IP;//要远程登录的IP地址
  private String username;//用户名
  private String password;//密码
  public ExecShell(String IP, String username,String password){
    this.IP=IP;
    this.username=username;
    this.password=password;
  }
  //命令执行
  public boolean exec( String command ) throws InterruptedException{
    Log.info("command: "+command);
    boolean rtn = false;
    try {
      Connection conn = new Connection(IP);
      conn.connect();
      boolean isAuthenticated = conn.authenticateWithPassword(username, password);
      if (isAuthenticated == false){
        throw new IOException("Authentication failed.");
      }
      Session sess = conn.openSession();
      sess.execCommand(command);
      InputStream stdout = new StreamGobbler(sess.getStdout());
      BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
      InputStream stderr = new StreamGobbler(sess.getStderr());
      BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
      String line = null;
      while ( (line = br.readLine())!=null )
      {
        Log.info("GanyMedUtil out> "+line);
      }
      while (true)
      {
        line = stderrReader.readLine();
        if (line == null)
          break;
        Log.info("GanyMedUtil out> "+line);
      }
      /* Show exit status, if available (otherwise "null") */
      Log.info("ExitCode: " + sess.getExitStatus()+" "+IP+":"+command);
      sess.close();
      conn.close();
      rtn = new Integer(0).equals(sess.getExitStatus());
      return rtn;
    }
    catch (IOException e)
    {
      Log.warn("Error ......................",e);
      e.printStackTrace();
      System.exit(2);
      return rtn;
    }
  }
  public static void main(String[] args) throws InterruptedException {
    ExecShell es = new ExecShell("192.168.75.130","root","dongliang");
    System.out.println("==========================================单个命令测试执行==========================================");
    es.exec("ls");//执行单行命令
    System.out.println("==========================================多个命令测试执行==========================================");
    es.exec("cd /root/apache-nutch-1.8/ && ls && date");//多个命令之间使用&&隔开
    //ganyMedUtil.execMoreShellCommand("");
    //ganyMedUtil.exec("ls");
  }
}

输出的内容如下所示:

2014-05-28 15:27:19.076:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
2014-05-28 15:27:19.076:INFO::command: ls
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> 2
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> abc.txt
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> anaconda-ks.cfg
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> apache-ant-1.9.2
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> apache-ant-1.9.2-bin.tar.gz
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> apache-nutch-1.8
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> apache-nutch-1.8-src.zip
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> a.sh
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> bb.sh
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> b.sh
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> case.sh
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> cip.sh
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> c.sh
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> date
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> d.sh
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> e.sh
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> f.sh
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> ganglia_20140327155418.zip
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> ganglia-3.1.7-3.el6.rf.x86_64.rpm
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> ganglia-devel-3.1.7-3.el6.rf.x86_64.rpm
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> ganglia-gmetad-3.1.7-3.el6.rf.x86_64.rpm
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> ganglia-gmond-3.1.7-3.el6.rf.x86_64.rpm
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> ganglia-gmond-python-3.1.7-3.el6.rf.x86_64.rpm
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> ganglia-web-3.1.7-3.el6.rf.x86_64.rpm
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> gg.sh
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> hadoop1.2
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> hadoop-1.2.0.tar.gz
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> installganglia.sh
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> install.log
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> install.log.syslog
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> jdk1.7
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> jdk-7u51-linux-x64.tar.gz
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> jmxtrans-20121016.145842.6a28c97fbb-0.noarch.rpm
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> libconfuse-2.7-6.2.x86_64.rpm
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> libevent-2.0.21-stable
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> libevent-2.0.21-stable.tar.gz
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> memcached-1.4.15
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> memcached-1.4.15.tar.gz
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> my.sh
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> pp
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> solr-4.3.1.tgz
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> test.jar
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> test.py
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> zookeeper
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> zookeeper.zip
2014-05-28 15:27:19.456:INFO::ExitCode: 0 192.168.75.130:ls
==========================================多个命令测试执行==========================================
2014-05-28 15:27:19.458:INFO::command: cd /root/apache-nutch-1.8/ && ls && date
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> bb.sh
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> build
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> build.xml
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> CHANGES.txt
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> conf
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> default.properties
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> docs
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> ivy
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> lib
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> LICENSE.txt
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> NOTICE.txt
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> README.txt
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> runtime
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> src
2014-05-28 15:27:19.790:INFO::GanyMedUtil out> 2014年 05月 28日 星期三 23:27:19 CST
2014-05-28 15:27:19.790:INFO::ExitCode: 0 192.168.75.130:cd /root/apache-nutch-1.8/ && ls && date

能够在在应用程序中,执行shell脚本是非常有用的,我们可以自己写一个shell脚本,把多个shell命令,封装起来,然后执行一次shell即可。
(原文链接