Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure java environment variables in linux

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces how to configure java environment variables in linux, the content is very detailed, interested friends can refer to, hope to be helpful to you.

one。 Extract and install jdk

Under the shell terminal, enter the directory where the jdk-6u14-linux-i586.bin file is located.

Execute the command. / jdk-6u14-linux-i586.bin at this time, there will be an agreement, continue to hit enter, when asked if you agree, type yes, enter. A jdk1.6.0_14 directory is then generated under the current directory, and you can copy it to any directory.

two。 Environment variables that need to be configured

1. PATH environment variable. The function is to specify the command search path, and when executing the command under shell, it will look in 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 wait. After setting the PATH variable, you can execute tools such as javac/java in any directory.

2. CLASSPATH environment variable. The role is to specify the class search path, to use the already written classes, of course, as long as they can be found, JVM is through the CLASSPTH to find the class. We need to set dt.jar and tools.jar in the lib subdirectory under the jdk installation directory to CLASSPATH and, of course, the current directory. " You must also add to this variable.

3. JAVA_HOME environment variable. It points to the installation directory of jdk, and software such as Eclipse/NetBeans/Tomcat searches the JAVA_HOME variable to find and use the installed jdk.

three。 Three ways to configure environment variables 1. Modify / etc/profile file

This method is recommended if your computer is used only as a developer, because all users' shell has access to these environment variables, which may cause security problems to the system.

Open / etc/profile with a text editor

Add the following at the end of the profile file:

The code is as follows:

Export JAVA_HOME=/usr/share/jdk1.6.0_14

Export PATH=$JAVA_HOME/bin:$PATH

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

Log in again

Notes

a. You need to change / usr/share/jdk1.6.0_14 to your jdk installation directory

B. separate paths with a colon ":" under linux

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

When setting environment variables, you should pay special attention to not overwriting the original value, which is a

Common mistakes.

D. the current directory in CLASSPATH "." You can't throw it away. Throwing away the current directory is also a common mistake.

E. export exports these three variables as global variables.

f. Case must be strictly distinguished.

two。 Modify the .bash _ profile file

This method is more secure, it can control the permissions to use these environment variables to the user level, if you need to give a user permission to use these environment variables, you only need to modify the. bash_profile file under the home directory of its individual user.

Open the. bash_profile file in the user directory with a text editor

At the end of the .bash _ profile file, add:

The code is as follows:

Export JAVA_HOME=/usr/share/jdk1.6.0_14

Export PATH=$JAVA_HOME/bin:$PATH

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

Log in again

3. Set variables directly under shell

Do not approve of using this method, because with a different shell, your setting is invalid, so this method is only used temporarily, and it is troublesome to reset it when you want to use it later.

Simply execute the following command on the shell terminal:

Export JAVA_HOME=/usr/share/jdk1.6.0_14

Export PATH=$JAVA_HOME/bin:$PATH

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

four。 Test jdk

1. Create a new Test.java file with a text editor, enter the following code in it, and save it:

The code is as follows:

Public class test {

Public static void main (String args []) {

System.out.println ("A new jdk test!")

}

}

two。 Compile: execute the command javac Test.java on the shell terminal

3. Run: execute the command java Test on the shell terminal

When "A new jdk test!" appears under shell The word jdk is running normally.

five。 Uninstall jdk

Find the _ unst subdirectory of the jdk installation directory

Execute the command. / uninstall.sh at the shell terminal to uninstall jdk.

On how to configure java environment variables in linux to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report