在centos下,默认装的是openjdk的 jre版本。

可以使用java 但不能使用 javac

[root@superguy ~]# rpm -qa | grep jdk 
[root@superguy ~]# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
[root@superguy ~]# javac
bash: javac: command not found...
Similar command is: 'java'
[root@superguy ~]# java
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)
where options include:
    -d32	  use a 32-bit data model if available
    -d64	  use a 64-bit data model if available
    -server	  to select the "server" VM
                  The default VM is server.

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A : separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
                  set a system property
                  enable verbose output
    -version      print product version and exit
                  Warning: this feature is deprecated and will be removed
                  in a future release.
                  require the specified version to run
    -showversion  print product version and continue
    -jre-restrict-search | -no-jre-restrict-search
                  Warning: this feature is deprecated and will be removed
                  in a future release.
                  include/exclude user private JREs in the version search
    -? -help      print this help message
    -X            print help on non-standard options
                  enable assertions with specified granularity
                  disable assertions with specified granularity
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
                  load native agent library <libname>, e.g. -agentlib:hprof
                  see also, -agentlib:jdwp=help and -agentlib:hprof=help
                  load native agent library by full pathname
                  load Java programming language agent, see java.lang.instrument
                  show splash screen with specified image
See for more details.
[root@superguy ~]#

后来查询发现需要装 openjdk 的devel 版本

[root@superguy ~]# yum list |grep jdk
copy-jdk-configs.noarch                  3.3-10.el7_5                   @updates
java-1.7.0-openjdk.x86_64                1:     @updates
java-1.7.0-openjdk-headless.x86_64       1:     @updates
java-1.8.0-openjdk.x86_64                1:        @updates
java-1.8.0-openjdk-headless.x86_64       1:        @updates
java-1.6.0-openjdk.x86_64                1:     base    
java-1.6.0-openjdk-demo.x86_64           1:     base    
java-1.6.0-openjdk-devel.x86_64          1:     base    
java-1.6.0-openjdk-javadoc.x86_64        1:     base    
java-1.6.0-openjdk-src.x86_64            1:     base    
java-1.7.0-openjdk-accessibility.x86_64  1:     updates 
java-1.7.0-openjdk-demo.x86_64           1:     updates 
java-1.7.0-openjdk-devel.x86_64          1:     updates 
java-1.7.0-openjdk-javadoc.noarch        1:     updates 
java-1.7.0-openjdk-src.x86_64            1:     updates 
java-1.8.0-openjdk.i686                  1:        updates 
java-1.8.0-openjdk-accessibility.i686    1:        updates 
java-1.8.0-openjdk-accessibility.x86_64  1:        updates 
java-1.8.0-openjdk-debug.i686            1:        updates 
java-1.8.0-openjdk-debug.x86_64          1:        updates 
java-1.8.0-openjdk-demo.i686             1:        updates 
java-1.8.0-openjdk-demo.x86_64           1:        updates 
java-1.8.0-openjdk-demo-debug.i686       1:        updates 
java-1.8.0-openjdk-demo-debug.x86_64     1:        updates 
java-1.8.0-openjdk-devel.i686            1:        updates 
java-1.8.0-openjdk-devel.x86_64          1:        updates

先更新 openjdk的jre版本,然后下载最新的 openjdk的devel版本。这个版本可以运行 javac

yum install java-1.8.0-openjdk-devel


一、Installing Java

Before installing Tomcat we will have to install Java Development Kit (JDK) on our system but it is recommended that you update the system and available packages before installing JDK. Run the following command to update system.

yum -y update

Now once the system is updated install JDK and to do so run following command:

yum -y install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64

The above command will take some time to install JDK so you should wait until the installation process has finished.

When Java is installed check the java version with the following command:

java -version

You’ll see a result similar to this :

[root@ip-172-31-16-36 ~]# java -version
openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-b15)
OpenJDK 64-Bit Server VM (build 25.111-b15, mixed mode)
[root@ip-172-31-16-36 ~]#

二、Java Home Environment

Before you configure Java Home Environment we recommend you to check where the Java directory is and to do so run following command:

sudo update-alternatives --config java

Now edit the environment file with any text editor here we are using nano editor but you can choose anyone you wish.

nano /etc/environment

Now add the Java Home Environment variable as shown below :


You can simply copy/paste this to your file.

After adding Java Home Environment save and exit from the editor.

Now we’ll have to edit the .bash_profile so run the command given below:

nano ~/.bash_profile

Now add the Home Environment variable as shown below:

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk- export PATH=$JAVA_HOME/bin:$PATH

Save file and exit from the editor.

We recommend you reload the .bash_profile, to do so run following command:

source ~/.bash_profile

We recommend you make sure there is no error in configuring Home Environment variable and also check the Java Home environment variable :


You will see output similar to this containing the path to java directory :

[root@ip-172-31-16-36 ~]# nano ~/.bash_profile
[root@ip-172-31-16-36 ~]# source ~/.bash_profile
[root@ip-172-31-16-36 ~]# echo $JAVA_HOME
[root@ip-172-31-16-36 ~]#


0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments