linux使用yum安装jdk

1. 看Linux系统是否有自带的jdk:

1、输入:java -version
2、发现有输入:rpm -qa | grep java 检测jdk的安装包,(注意:rpm命令符没有时记得下载一个输入:apt-get install +命令名称)
3、接着进行一个个删除包,输入:rpm -e –nodeps +包名
4、最后再次:rpm -qa | grep java检查是否删除完即可

2、查询要安装jdk的版本

命令:yum -y list java*

3、安装所需版本

命令:yum install -y java-1.8.0-openjdk.devel.x86_64

注意这里一定要安装devel版本, 否则会出现只安装了jre的情况导致无法使用javac命令

4. 查看是否安装成功

javac, java -version

javac命令报错bash:javac:command not found | yum provide javac

问题分析:自带的jdk默认只安装了运行环境(找到jdk的安装目录下,发现只有jre文件夹,没有bin、lib等文件夹)。

自查方法

(1)先找到jdk的安装目录

[root@localhost home]# which java
/usr/bin/java
[root@localhost home]# ls -lrt /usr/bin/java
lrwxrwxrwx. 1 root root 22 Apr 18 13:09 /usr/bin/java -> /etc/alternatives/java
[root@localhost home]# ls -lrt /etc/alternatives/java
lrwxrwxrwx. 1 root root 73 Apr 18 13:09 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64/jre/bin/java12345

这就是jdk安装的文件夹:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64

(2)打开这个文件夹,检查它的子文件夹。如果只有jre一个文件夹,那就是需要安装devel开发环境。

[root@localhost home]# cd /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64
[root@localhost java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64]# ll
total 0
drwxr-xr-x. 4 root root 28 Apr 18 13:09 jre123

解决方法:使用yum安装开发环境 devel。(注意下载对应的版本。所以最好用yum找到准确的devel版本名字,精准下载)


0. 检查自己的openjdk版本,发现是1.8.0
(如果没有安装openjdk,则用yum安装openjdk之后,再来安装一次devel即可)
【参考教程linux 上使用yum 安装openjdk1.8

[root@localhost ~]# java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)
1234

1. 用yum查找可以安装的jdk:yum search java|grep jdk

[root@localhost java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64]# yum search java|grep jdk
openjdk.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk.i686 : OpenJDK Runtime Environment 8
java-1.8.0-openjdk-accessibility.i686 : OpenJDK accessibility connector
java-1.8.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector
java-1.8.0-openjdk-accessibility-debug.i686 : OpenJDK 8 accessibility connector
java-1.8.0-openjdk-accessibility-debug.x86_64 : OpenJDK 8 accessibility
java-1.8.0-openjdk-debug.i686 : OpenJDK Runtime Environment 8 with full debug on
java-1.8.0-openjdk-debug.x86_64 : OpenJDK Runtime Environment 8 with full debug
java-1.8.0-openjdk-demo.i686 : OpenJDK Demos 8
java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos 8
java-1.8.0-openjdk-demo-debug.i686 : OpenJDK Demos 8 with full debug on
java-1.8.0-openjdk-demo-debug.x86_64 : OpenJDK Demos 8 with full debug on
java-1.8.0-openjdk-devel.i686 : OpenJDK Development Environment 8
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment 8
java-1.8.0-openjdk-devel-debug.i686 : OpenJDK Development Environment 8 with
java-1.8.0-openjdk-devel-debug.x86_64 : OpenJDK Development Environment 8 with
java-1.8.0-openjdk-headless.x86_64 : OpenJDK Runtime Environment1234567891011121314151617

(有很多很多文件,这里删减了一部分)
找到了我需要的版本:java-1.8.0-openjdk-devel.x86_64。

2. 安装java-1.8.0-openjdk-devel.x86_64:
yum install java-1.8.0-openjdk-devel.x86_64

这时候会有提示:Is this ok [y/d/N]:
输入y (y的意思是-下载并安装)。

然后静待下载…complete!

3.检查jdk目录下是否有了bin、lib等文件夹:

[root@localhost home]# cd /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64
[root@localhost java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64]# ll
total 4
drwxr-xr-x. 2 root root 4096 Apr 18 13:09 bin
drwxr-xr-x. 3 root root 132 Apr 18 13:09 include
drwxr-xr-x. 4 root root 28 Apr 18 13:09 jre
drwxr-xr-x. 3 root root 144 Apr 18 13:09 lib
drwxr-xr-x. 2 root root 204 Apr 18 13:09 tapset
12345678

有了!
再次输入javac,没有报错:

[root@localhost jvm]# javac
Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files and annotation processors
-cp <path> Specify where to find user class files and annotation processors
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-endorseddirs <dirs> Override location of endorsed standards path
-proc:{none,only} Control whether annotation processing and/or compilation is done.
-processor <class1>[,<class2>,<class3>...] Names of the annotation processors to run; bypasses default discovery process
-processorpath <path> Specify where to find annotation processors
-parameters Generate metadata for reflection on method parameters
-d <directory> Specify where to place generated class files
-s <directory> Specify where to place generated source files
-h <directory> Specify where to place generated native header files
-implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files
-encoding <encoding> Specify character encoding used by source files
-source <release> Provide source compatibility with specified release
-target <release> Generate class files for specific VM version
-profile <profile> Check that API used is available in the specified profile
-version Version information
-help Print a synopsis of standard options
-Akey[=value] Options to pass to annotation processors
-X Print a synopsis of nonstandard options
-J<flag> Pass <flag> directly to the runtime system
-Werror Terminate compilation if warnings occur
@<filename> Read options and filenames from file