tomcat(二)安装openjdk

By | 2019年 11月 23日

1. 安装openjdk

  1. 在Centos中,可以使用yum安装openjdk。
yum install java-1.8.0-openjdk java-1.8.0-openjdk-devel -y 
  1. 查看版本
# java -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-b04)
OpenJDK 64-Bit Server VM (build 25.212-b04, mixed mode)
  1. 编写 Hellworld 程序进行测试
vim hello.java
public class hello{
        public static void main(String[] args){
                System.out.println("hello world magedu !");
        }
}

这个类名hello要和文件名一致

  1. 编译 运行
# javac hello.java
# java hello
hello world magedu !
  1. 了解下java和javac两个程序的路径
# hash
hits    command
   3    /usr/bin/javac
   1    /usr/bin/df
   5    /usr/bin/vim
   5    /usr/bin/java
   1    /usr/bin/yum
   9    /usr/bin/ls

可以看到javac是/usr/bin/javac/usr/bin/java,再跟踪去看看

# ll /usr/bin/javac
lrwxrwxrwx 1 root root 23 Sep  2 09:40 /usr/bin/javac -> /etc/alternatives/javac
# ll /usr/bin/java
lrwxrwxrwx 1 root root 22 Sep  2 09:40 /usr/bin/java -> /etc/alternatives/java

指向了/etc/alternatives这个目录,再去看看

# ll /etc/alternatives/javac
lrwxrwxrwx 1 root root 68 Sep  2 09:40 /etc/alternatives/javac -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-7.b13.el7.x86_64/bin/javac
# ll /etc/alternatives/java
lrwxrwxrwx 1 root root 71 Sep  2 09:40 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-7.b13.el7.x86_64/jre/bin/java

其实就是前面安装那2个包的路径

#  rpm -ql java-1.8.0-openjdk java-1.8.0-openjdk-devel | grep /bin/java$
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-7.b13.el7.x86_64/bin/java
# rpm -ql java-1.8.0-openjdk java-1.8.0-openjdk-devel | grep /bin/javac$
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-7.b13.el7.x86_64/bin/javac

1.1. yum安装 oracle jdk

  1. 需要下载这个rpm包安装
yum install jdk-8u211-linux-x64.rpm

官网:https://download.oracle.com/otn/java/jdk

  1. 查看版本
# java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
  1. 编写 Hellworld 程序进行测试
vim hello.java
public class hello{
        public static void main(String[] args){
                System.out.println("hello world magedu !");
        }
}

这个类名hello要和文件名一致

  1. 编译执行
# javac hello.java
# java hello
hello world magedu

1.2. Linux版二进制安装oracle jdk

  1. 下载二进制包,解压,改名
tar xf jdk-8u211-linux-x64.tar.gz -C /usr/local/
mv jdk1.8.0_211 jdk1.8

官网:https://download.oracle.com/otn/java/jdk

  1. 安装目录为/usr/local/下,所以Java全局配置如下
vim /etc/profile.d/jdk.sh

BashCopy

export JAVA_HOME=/usr/local/jdk1.8
export PATH=$JAVA_HOME/bin:$PATH
  1. 加载下
. /etc/profile.d/jdk.sh
  1. 查看下版本
# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
  1. 编写 Hellworld 程序进行测试
vim hello.java
public class hello{
        public static void main(String[] args){
                System.out.println("hello world magedu !");
        }
}

这个类名hello要和文件名一致

  1. 编译执行
# javac hello.java
# java hello
hello world magedu

2. 安装tomcat

可以使用Centos7 yum源自带的安装。yum源中是Tomcat 7.0版本。安装完通过浏览器可以观察一下首页。
1. yum安装

yum -y install tomcat tomcat-admin-webapps tomcat-webapps
  1. 查看版本
java -version
  1. 创建测试 index.html 并启动 tomcat 进行测试
echo '<h1>hello tomcatttt</h1>' > /var/lib/tomcat/webapps/ROOT/index.html
  1. 启动服务
systemctl start tomcat.service
systemctl enable tomcat.service
  1. 查看端口(8080和8009)
# ss -tnl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN     0      128      *:22                   *:*
LISTEN     0      100     :::8009                :::*
LISTEN     0      100     :::8080                :::*
LISTEN     0      128     :::22                  :::*
  1. 本机测试下
# curl localhost:8080
<h1>hello tomcatttt</h1>

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注