Linux

Java基础——环境配置 - 图1

1安装openjdk

官方地址http://openjdk.java.net/ ,如果需要开发java程序则需要安装jdk
Debian, Ubuntu, 安装jdk

  1. sudo apt-get install openjdk-8-jdk #8的版本
  2. sudo apt-get install openjdk-7-jdk #7的版本

Fedora, Oracle Linux, Red Hat Enterprise Linux,的安装方式

  1. $ su -c "yum install java-1.8.0-openjdk-devel"
  2. $ su -c "yum install java-1.7.0-openjdk-devel"

2安装Oracle Java SE

卸载openjdk 通过rpm -qa | grep java 命令查找openjdk

  1. rpm -qa | grep java
  2. python-javapackages-3.4.1-11.el7.noarch
  3. tzdata-java-2018e-3.el7.noarch
  4. javapackages-tools-3.4.1-11.el7.noarch
  5. java-1.8.0-openjdk-headless-1.8.0.181-3.b13.el7_5.x86_64
  6. java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64

通过rpm -e —nodeps (安装包名称) 命令依次卸载

  1. rpm -e --nodeps python-javapackages-3.4.1-11.el7.noarch
  2. rpm -e --nodeps tzdata-java-2018e-3.el7.noarch
  3. rpm -e --nodeps javapackages-tools-3.4.1-11.el7.noarch
  4. rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.181-3.b13.el7_5.x86_64
  5. rpm -e --nodeps java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64

删完之后可以再通过 rpm -qa | grep Java 命令来查询出是否删除掉
java官方服务器下载地址http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Java基础——环境配置 - 图2
由于必须接受Accept License Agreement才能下载,所有需要在wget增加cookie

  1. wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u181-linux-x64.rpm"

安装rpm的命令 rpm -ivh

  1. rpm -ivh jdk-8u181-linux-x64.rpm
  2. warning: jdk-8u181-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
  3. Preparing... ################################# [100%]
  4. Updating / installing...
  5. 1:jdk1.8-2000:1.8.0_181-fcs ################################# [100%]
  6. Unpacking JAR files...
  7. tools.jar...
  8. plugin.jar...
  9. javaws.jar...
  10. deploy.jar...
  11. rt.jar...
  12. jsse.jar...
  13. charsets.jar...
  14. localedata.jar...
  1. export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
  2. PATH=$PATH:$JAVA_HOME/bin

验证安装结果

  1. java -version
  2. java version "1.8.0_181"
  3. Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
  4. Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

windows 需要把JDK路径添加到系统环境变量PATH

JRE

jre是java的运行环境
Debian, Ubuntu, 安装jre

  1. sudo apt-get install openjdk-8-jre #8的版本
  2. sudo apt-get install openjdk-7-jre #7的版本

Fedora, Oracle Linux, Red Hat Enterprise Linux,的安装方式

  1. $ su -c "yum install java-11-openjdk"
  2. $ su -c "yum install java-1.8.0-openjdk"
  3. $ su -c "yum install java-1.7.0-openjdk"

编写第一个JAVA程序

  1. public class Main {
  2. public static void main(String[] args) {
  3. System.out.println("Hello World!");
  4. }
  5. }

编译javac main.java

  1. $ javac Main.java
  2. baxiangs-Mac-mini:src baxiang$ tree
  3. .
  4. ├── Main.class
  5. └── Main.java
  6. 0 directories, 2 files

运行Java程序

  1. $ java Main
  2. Hello World!

mac

执行/usr/libexec/java_home -V查看系统已安装的JDK

  1. /usr/libexec/java_home -V

参考

https://blog.csdn.net/cloud_xy/article/details/88623975
https://java.tutorials24x7.com/blog/how-to-install-openjdk-15-on-mac