Configure JDK in CentOS and JDK in CentOS

Source: Internet
Author: User

Configure JDK in CentOS and JDK in CentOS

Download the jdk from the official website to the system and decompress it.

Tar-axvf jdk.tr.gz

 

1. PATH environment variable. The function is to specify the command search PATH. When you execute a command under shell, it searches for the PATH specified by the PATH variable to see if the corresponding command program can be found. We need to add the bin directory under the jdk installation directory to the existing PATH variable. The bin directory contains frequently used executable files such as javac/java/javadoc waiting, after setting the PATH variable, You can execute tools such as javac and java in any directory.

 

2. CLASSPATH environment variable. The function is to specify the Class search path and use the classes that have already been compiled. The premise is that you can find them, and JVM searches for classes through CLASSPTH. We need to set dt. jar and tools. jar in the lib subdirectory under the jdk installation directory to CLASSPATH. Of course, the current directory "." must also be added to this variable.

 

3. JAVA_HOME environment variable. It points to the jdk installation directory. Eclipse, NetBeans, Tomcat, and other software find and use the installed jdk by searching the JAVA_HOME variable.

 

Iv. Three Methods for configuring Environment Variables

 

1. Modify the/etc/profile file

 

This method is recommended if your computer is used only for development, because all users' shells have the right to use these environment variables, which may bring security issues to the system.

 

• Open/etc/profile in a text editor

• Add the following content to the end of the profile file:

 

JAVA_HOME =/usr/share/jdk1.5.0 _ 05

PATH = $ JAVA_HOME/bin: $ PATH

CLASSPATH =.: $ JAVA_HOME/lib/dt. jar: $ JAVA_HOME/lib/tools. jar

Export JAVA_HOME

Export PATH

Export CLASSPATH

 

• Log on again or source/etc/profile

• Annotation

A. Change/usr/share/jdk1.5.0 _ 05jdk to your jdk installation directory.

B. Use the colon ":" To separate paths in linux

C. $ PATH/$ CLASSPATH/$ JAVA_HOME is used to reference the value of the original environment variable.

When setting environment variables, pay special attention not to overwrite the original values. This is

Common errors.

D. The current directory "." In CLASSPATH cannot be lost. It is also a common error to discard the current directory.

E. export exports the three variables as global variables.

F. Case sensitivity is required.

 

2. Modify the. bashrc File

    

This method is more secure. It can control the permissions to use these environment variables to the user level. If you need to grant a user permission to use these environment variables, you only need to modify. you can use the bashrc file.

 

• Open the. bashrc file in the user directory in a text editor

If you enter the graphic interface, you will not be able to see the file. Press Ctrl + h to display the hidden file. You can open it in a text editor or open it through Vi.

• Add the following content to the end of the. bashrc file:

      

Set JAVA_HOME =/usr/share/jdk1.7.0 _ 25

Export JAVA_HOME

Set PATH = $ JAVA_HOME/bin: $ PATH

Export PATH

Set CLASSPATH =.: $ JAVA_HOME/lib/dt. jar: $ JAVA_HOME/lib/tools. jar

Export CLASSPATH

 

• Log on again

Use commands

Source ~ /. Bashrc

Make it take effect

 

3. directly set variables in shell

 

I am not in favor of using this method. If you change the shell, your settings will be invalid. Therefore, this method is only for temporary use and will be re-set later, which is troublesome.

 

Run the following command on the shell terminal:

 

Export JAVA_HOME =/usr/share/jdk1.5.0 _ 05

Export PATH = $ JAVA_HOME/bin: $ PATH

Export CLASSPATH =.: $ JAVA_HOME/lib/dt. jar: $ JAVA_HOME/lib/tools. jar

In this way, the jdk environment variables are configured.

Maylorchao @ TerryLMay :~ $ Java-version

Java version "1.6.0 _ 27"

OpenJDK Runtime Environment (IcedTea6 1.12.5) (6b27-1.12.5-0ubuntu0.12.04.1)

OpenJDK Server VM (build between 0-b12, mixed mode)

Maylorchao @ TerryLMay :~ $

In this way, jdk configuration has not been completed in a real sense. You should know that the system has a set of openJdk, which is the default of the system.

The current java environment in the system shows that the default environment of the system is 1.6 .. this is not the version 1.7 I just installed .. later, we can see that the java package is OpenJDK.

 

The subsequent work is to use the configured jdk instead of the default openJdk ..

4. Configure the default JDK

The default JDK exists in some Linux distributions, such as OpenJDK. So in order to make the installed JDK version become the default JDK version, we need to make the following configuration.

Run the following command:

Sudo update-alternatives -- install/usr/bin/java/usr/lib/jvm/jdk7/bin/java 300

Sudo update-alternatives -- install/usr/bin/javac/usr/lib/jvm/jdk7/bin/javac 300

Note: If the above two commands fail to find the path, restart the computer and repeat the above two lines of code.

In the command line, press sudo update-alternatives -- config java scripts to display the following text:

Sudo update-alternatives -- config java

Two candidate items can be used to replace java (/usr/bin/java ).

 

Select the path priority status

------------------------------------------------------------

* 0/usr/lib/jvm/java-6-openjdk-i386/jre/bin/java 1061 automatic mode

1/usr/lib/jvm/java-6-openjdk-i386/jre/bin/java 1061 manual mode

2/usr/share/jdk1.7.0 _ 25/bin/java 300 manual mode

 

To maintain the current value [*], press the Enter key or enter the selected number:

 

In this way, you can set the default java version of the system by selecting number 2 .. as for testing, I don't think it is necessary, because you just need to input javac and java commands in the terminal to see if there is any help information .. try it? :

Maylorchao @ TerryLMay :~ $ Javac

Usage: javac <option> <Source File>

Possible options include:

-G generates all debugging information

-G: none does not generate any debugging information

-G: {lines, vars, source} only generates some debugging information

-Nowarn does not generate any warning

-Verbose outputs messages about the operations being performed by the compiler.

-Deprecation: output the source location of an outdated API

-Classpath <path> specifies the location for searching user class files and annotation handlers.

-Cp <path> specifies the location for searching user class files and annotation handlers

-Sourcepath <path> specifies the location of the Input Source file to be searched.

-Bootclasspath <path> overwrites the position of the boot class file

-Extdirs <directory> overwrites the location of the installed extension directory.

-Endorseddirs <directory> overwrites the location of the standard path of the signature.

-Proc: {none, only} controls whether to perform annotation processing and/or compilation.

-Processor <class1> [, <class2>, <class3>...] Name of the annotation handler to run; bypassing the default search process

-Processorpath <path> specifies the location of the comment handler.

-D <directory> specifies the location where the generated class files are stored.

-S <directory> specifies the location where the generated source file is stored

-Implicit: {none, class} specifies whether to generate class files for implicitly referenced files

-Encoding <encoding> specifies the character encoding used by the source file.

-Source <version> provides source compatibility with the specified version.

-Target <version> generates class files of a specific VM version.

-Version Information

-Help: output the summary of standard options.

-Akey [= value] Options passed to the annotation Handler

-X outputs a feed of non-standard options

-J <flag> directly transmits the <flag> to the runtime system

-Werror Terminate compilation if warnings occur

 

Maylorchao @ TerryLMay :~ $

 

Java running environment is normal @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

 

Maylorchao @ TerryLMay :~ $ Javac

Usage: javac <option> <Source File>

Possible options include:

-G generates all debugging information

-G: none does not generate any debugging information

-G: {lines, vars, source} only generates some debugging information

-Nowarn does not generate any warning

-Verbose outputs messages about the operations being performed by the compiler.

-Deprecation: output the source location of an outdated API

-Classpath <path> specifies the location for searching user class files and annotation handlers.

-Cp <path> specifies the location for searching user class files and annotation handlers

-Sourcepath <path> specifies the location of the Input Source file to be searched.

-Bootclasspath <path> overwrites the position of the boot class file

-Extdirs <directory> overwrites the location of the installed extension directory.

-Endorseddirs <directory> overwrites the location of the standard path of the signature.

-Proc: {none, only} controls whether to perform annotation processing and/or compilation.

-Processor <class1> [, <class2>, <class3>...] Name of the annotation handler to run; bypassing the default search process

-Processorpath <path> specifies the location of the comment handler.

-D <directory> specifies the location where the generated class files are stored.

-S <directory> specifies the location where the generated source file is stored

-Implicit: {none, class} specifies whether to generate class files for implicitly referenced files

-Encoding <encoding> specifies the character encoding used by the source file.

-Source <version> provides source compatibility with the specified version.

-Target <version> generates class files of a specific VM version.

-Version Information

-Help: output the summary of standard options.

-Akey [= value] Options passed to the annotation Handler

-X outputs a feed of non-standard options

-J <flag> directly transmits the <flag> to the runtime system

-Werror Terminate compilation if warnings occur

 

Maylorchao @ TerryLMay :~ $

 

Normal javac compiling environment @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

 

Maylorchao @ TerryLMay :~ $ Java-version

Java version "1.7.0 _ 25"

Java (TM) SE Runtime Environment (build 1.7.0 _ 25-b15)

Java HotSpot (TM) Server VM (build 23.25-b01, mixed mode)

Maylorchao @ TerryLMay :~ $ ^ C

 

By using java-version, we can see that the default java environment has changed to the jdk1.7 version I Just configured ..

In this way, jdk configuration in linux is complete...

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.