Detailed steps for installing and configuring java environment variables in Linux CentOS 7.0

  • 2020-10-07 18:58:17
  • OfStack

1. Introduction:

CentOS7.0 although it comes with JDK1.7 and 1.8, you can also see the version information by running the "java-ES6en" command, but the installation environment of jdk is incomplete, such as the lack of tool.jar and dt.jar, etc., which results in the command like "javac" can't be used even if the environment variable is configured. Therefore, you need to reinstall jdk and configure the environment variable, and solve the problem that the "javac" command is not available.

The following words do not say much, come with this site 1 start to see the detailed introduction.

2. Download and install JDK

1. Under CentOS, first let the current user obtain root permissions and open the terminal to input commands:


su root

2. View the installable version of JDK:

The list of query results is as follows:


java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.7.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector
java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.7.0-openjdk-headless.x86_64 : The OpenJDK runtime environment without audio and video support
java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle

3. Install this one, command:


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

Tips for completion of Installation:

[

Installed:
java-1.7.0-openjdk-devel.x86_64 1:1.7.0.141-2.6.10.1.el7_3

Be upgraded as a dependency:
java-1.7.0-openjdk.x86_64 1:1.7.0.141-2.6.10.1.el7_3 java-1.7.0-openjdk-headless.x86_64 1:1.7.0.141-2.6.10.1.el7_3 nspr.x86_64 0:4.13.1-1.0.el7_3 nss.x86_64 0:3.28.4-1.2.el7_3
nss-sysinit.x86_64 0:3.28.4-1.2.el7_3 nss-tools.x86_64 0:3.28.4-1.2.el7_3 nss-util.x86_64 0:3.28.4-1.0.el7_3

Finished!

]

Remember the folder name after "installed" above, and don't get confused. We use the path of this file as JAVA_HOME. Remember that!

4. After installation, browse the directory after installation:

JDK is installed under the /usr/lib/jvm/ folder. There are multiple VERSIONS of JDK in this folder. Find the version we just installed through the version JDK in the prompt message of successful installation.

I find the newly installed JDK folder is: / usr/lib/jvm/java - 1.7.0 - openjdk - 1.7.0.141-2.6.10.1. el7_3. x86_64


cd /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.141-2.6.10.1.el7_3.x86_64/lib

ls

Query results:

[

amd64 ct.sym dt.jar ir.idl jconsole.jar jexec orb.idl sa-jdi.jar tools.jar

]

Finally, I saw the tool.jar and ES86en.jar packages I haven't seen for a long time. Because JDK comes with centOS 7.0 doesn't have any of these jar packages at all, the "javac" command won't work whether you configure your environment variables correctly or not because it relies on the ES92en.jar package.

5. Next, configure the environment variables:

Get the installation path of JDK:

In step 4, is our own folder/usr lib/jvm/java - 1.7.0 - openjdk - 1.7.0.141-2.6.10.1. el7_3. x86_64 / lib.

Enter the following two commands (note the space after cd) to get the installation directory of JDK:


cd .

pwd

This path is JAVA_HOME


/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.141-2.6.10.1.el7_3.x86_64

Use command to edit environment variable file:


vim /etc/profile

After opening the file, press 'E' on the keyboard to enter edit mode, and then press 'pagedown' a few times to get to the bottom of the file.

Change the following JAVA_HOME to the path of JAVA_HOME just obtained, and then copy to the bottom of the file.


export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.141-2.6.10.1.el7_3.x86_64
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

Explanation:

- Just change the end of line 1 after "export JAVA_HOME=" to your JDK path, taking care not to add "/" at the end. Nothing else needs to change.

The -ES147en command is used to output the shell variable as an environment variable

- ":" colon, used to split the path in linux, "; "in Windows Segmentation. , where JAVA_HOME and other environment variables are split ($PATH)

- The ". "in CLASSPATH cannot be lost. CLASSPATH directs to tools.jar to use the "javac" command in any directory

After typing, press "ESC" to switch to Vim command mode, enter the command (colon, press and hold "SHIFT+ :"), save and exit


:wq!

6. Make environment variables take effect immediately command:


source /etc/profile

7. Check whether environment variables are configured successfully:

Under any file path, the terminal enters the command:


java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.7.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector
java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.7.0-openjdk-headless.x86_64 : The OpenJDK runtime environment without audio and video support
java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle
0

The results are shown as follows. Environment variables are configured successfully:

[

[root@localhost java-1.7.0-openjdk-1.7.0.141-2.6.10.1.el7_3.x86_64]# javac
Usage: javac < options > < source files >
Among them, possible options include:
-g generates all debugging information
-g:none does not generate any debugging information
-ES200en :{lines,vars,source} only generates some debugging information
-ES205en does not generate any warnings
-verbose outputs a message about what the compiler is doing
-deprecation output USES the source location of the obsolete API
-classpath < The path > Specifies where to find user class files and comment handlers
-cp < The path > Specifies where to find user class files and comment handlers
-sourcepath < The path > Specifies where to find the input source file
-bootclasspath < The path > Overwrite the location of the boot class file
-extdirs < directory > Overwrites the location of the installed extension
-endorseddirs < directory > The location of the standard path to overwrite the signature
-ES236en :{none,only} controls whether annotation processing and/or compilation are performed.
-processor < class1 > [, < class2 > , < class3 > . ] The name of the annotation handler to run; Bypass the default search process
-processorpath < The path > Specifies where to find the annotation handler
-d < directory > Specifies where to place the generated class files
-s < directory > Specifies where to place the generated source files
-ES265en :{none,class} specifies whether to generate a class file for an implicit reference file
-encoding < coding > Specifies the character encoding used by the source file
-source < release > Provides source compatibility with the specified distribution
-target < release > Generate class files for the specific VM version
-version version information
-help outputs a feed of standard options
The -ES286en keyword [= value] option passed to the annotation handler
-ES288en outputs a feed of non-standard options
-J < tag > Directly to the < tag > To the runtime system
-Werror terminates compilation when warned
@ < The file name > Read options and filenames from files

]

If prompted, "bash: javac: command not found..." Check if the path of the environment variable is correct. After repeated attempts, good luck.

conclusion


Related articles: