Ubuntu——配置JDK

在Ubuntu下配置JDK环境

 

检查是否已经安装了JDK,执行以下命令:

java -v

如果出现以下内容则说明没有安装:

程序 'java' 已包含在下列软件包中:
 * default-jre
 * gcj-5-jre-headless
 * openjdk-8-jre-headless
 * gcj-4.8-jre-headless
 * gcj-4.9-jre-headless
 * openjdk-9-jre-headless
请尝试:sudo apt install <选定的软件包>

 

解压文件到目标目录

下载JDK的tar.gz包,然后将其解压之后,移动到如下路径:

/usr/lib/java

 

配置环境变量

修改配置文件:

/etc/profile

修改之前:

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "$PS1" ]; then
  if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

在末尾增加内容,修改后:

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "$PS1" ]; then
  if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

export JAVA_HOME=/usr/lib/java/jdk1.8.0_151
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

重新加载配置文件:

source /etc/profile

测试,执行命令查看java版本信息:

java -version

出现结果:

java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

此处需要注意,一开始命令写错了写成了 java -v,结果报了个错:

Unrecognized option: -v
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

此处应该关注的错误是第一行的内容,之前没有注意,结果走了半天弯路。

 

配置软链接

update-alternatives --install /usr/bin/java java /usr/lib/java/jdk1.8.0_151/bin/java 300
update-alternatives --install /usr/bin/javac javac /usr/lib/java/jdk1.8.0_151/bin/javac 300

 

posted @ 2017-11-08 14:56  大肥肥就是我  阅读(5864)  评论(0编辑  收藏  举报