最简单Linux 中java 环境配置

徘徊边缘 提交于 2019-11-25 21:50:08

系统环境(centos 7)

CentOS Linux release 7.7.1908 (Core)

目标

安装 jdk 1.8

原因

看了网上写的关于环境配置的文章,只能用一个字来评价“乱”,带来的效果就是误人子弟
只能整理一份自己常用的环境配置文件来分享给大家

首先,查看本机上是否有自带的java
(最简单就是通过java --version 看一下有没有)
没有的话就可以使用下面命令给你的新环境配置java 了,可以发现找到了很多可以安装的包,简单起见,我们全部安装(当然只安装其中的devel 版本也是可以的)

[root@VM_0_4_centos ~]# yum list java-1.8*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Available Packages
java-1.8.0-openjdk.i686                                                          1:1.8.0.232.b09-0.el7_7                                    updates 
java-1.8.0-openjdk-accessibility.i686                                            1:1.8.0.232.b09-0.el7_7                                    updates 
java-1.8.0-openjdk-accessibility-debug.i686                                      1:1.8.0.232.b09-0.el7_7                                    updates 
java-1.8.0-openjdk-debug.i686                                                    1:1.8.0.232.b09-0.el7_7                                    updates 
java-1.8.0-openjdk-demo.i686                                                     1:1.8.0.232.b09-0.el7_7                                    updates 
java-1.8.0-openjdk-demo-debug.i686                                               1:1.8.0.232.b09-0.el7_7                                    updates 
java-1.8.0-openjdk-devel.i686                                                    1:1.8.0.232.b09-0.el7_7                                    updates 
java-1.8.0-openjdk-devel-debug.i686                                              1:1.8.0.232.b09-0.el7_7                                    updates 
java-1.8.0-openjdk-headless.i686                                                 1:1.8.0.232.b09-0.el7_7                                    updates 
java-1.8.0-openjdk-headless-debug.i686                                           1:1.8.0.232.b09-0.el7_7                                    updates 
java-1.8.0-openjdk-src.i686                                                      1:1.8.0.232.b09-0.el7_7                                    updates 
java-1.8.0-openjdk-src-debug.i686                                                1:1.8.0.232.b09-0.el7_7                                    updates 
[root@VM_0_4_centos ~]# 

安装jdk 命令
(下载、安装需要部分时间,慢慢等待安装完即可)

[root@VM_0_4_centos ~]# yum install java-1.8* -y

安装完成之后默认可以使用java 命令了,但是javac 等常用jdk 命令还是不能使用,因为我们还没有配置java 运行环境变量【如果,只想使用java 命令,到这里就可以了,不用下面的配置也能使用了】

下面开始配置环境变量
通过下面命令找到系统环境配置文件

[root@VM_0_4_centos ~]# vim /etc/profile

打开profile 文件后,在文件最后添加下面三行

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar
export PATH=$PATH:$JAVA_HOME/bin

(注意其中的第一行 JAVA_HOME 中1.8.0.232 可能与你的显示不一样,版本问题无需关注,只需到 java 默认安装目录下 /usr/lib/jvm 复制你的文件夹名称替换掉即可)
java 默认安装目录
写完保存之后,通过下面命令让配置生效

[root@VM_0_4_centos jvm]# . /etc/profile

最后,验证是否安装成功
在这里插入图片描述

[root@VM_0_4_centos 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

以上
BR

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!