Home  >  Article  >  Java  >  How to configure Java environment variables in Linux?

How to configure Java environment variables in Linux?

WBOY
WBOYforward
2023-04-20 23:55:063882browse

1. Download a JDK for Linux Platform.

It is recommended to download the RPM self-extracting format (RPM in self-extracting file, j2sdk-1_4_2_06-linux-i586-rpm.bin);

2. Upload to the Linux server and execute the command under the shell:

[root@LinuxServer rpm]# chmod 755 j2sdk-1_4_2_11-linux-i586-rpm.bin    [root@LinuxServer rpm]# ./j2sdk-1_4_2_11-linux-i586-rpm.bin

There will be a section of Sun's agreement. Press the space bar several times. When asked whether you agree, just press yes. .

Sun Microsystems, Inc.   Binary Code License Agreement   for the   JAVATM 2 SOFTWARE DEVELOPMENT KIT (J2SDK), STANDARD   EDITION, VERSION 1.4.2_X   ...   Do you agree to the above license terms? [yes or no]yes   Unpacking...   Checksumming...   0   0   Extracting...   UnZipSFX 5.40 of 28 November 1998, by Info-ZIP (Zip-Bugs@lists.wku.edu).   inflating: j2sdk-1_4_2_11-linux-i586.rpm   Done.

Note: It is also possible to directly execute the unzip command to decompress. In short, you will get an rpm software package.

3. The program will automatically generate a j2sdk-1_4_2_11-linux-i586.rpm file , this is the main package, install it below;

[root@LinuxServer rpm]#rpm -ivh j2sdk-1_4_2_11-linux-i586.rpm   Preparing... ########################################### [100%]   1:j2sdk ########################################### [100%]

4. Set environment variables

Usually like to use the export command to set it directly under the shell

[root@LinuxServer rpm]# export JAVA_HOME=/usr/java/j2sdk1.4.2_11   [root@LinuxServer rpm]# export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar   [root@LinuxServer rpm]# export PATH=$PATH:$JAVA_HOME/bin

Of course, set it like this Environment variables can take effect, but only for the current shell. If you log in from another shell, you will not be able to use the variables you just set. So the best way is to modify the .bashrc file.

[root@LinuxServer rpm]#vi .bashrc   set JAVA_HOME=/usr/java/j2sdk1.4.2_11   export JAVA_HOME   set PATH=$PATH:$JAVA_HOME/bin   export PATH   set CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar   export CLASSPATH

Of course, it can also be achieved by changing /etc/profile, but this is not recommended because such settings will take effect on all user shells and will have an impact on system security. Just add:

export JAVA_HOME=/usr/java/j2sdk1.4.2_11   export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar   export PATH=$PATH:$JAVA_HOME/bin

to the *** of this file to verify whether the variable settings are effective (

#Note: logout before verification, and then log in again);

[root@LinuxServer rpm]# echo $JAVA_HOME   /usr/java/j2sdk1.4.2_11/   [root@LinuxServer rpm]# echo $CLASSPATH   /usr/java/j2sdk1.4.2_11/lib/dt.jar:/usr/java/j2sdk1.4.2_11/lib/tools.jar   [root@LinuxServer rpm]# echo $PATH   /usr/java/j2sdk1.4.2_11/bin/:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbi   n:   /usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin   [root@LinuxServer rpm]# JAVA-version   JAVA version "1.4.2_11"   JAVA(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_11)   JAVA HotSpot(TM) Client VM (build 1.4.2_11, mixed mode)
5. The environment settings are OK. Let's see if the JDK can work normally. Let's write a test file test.java

[root@LinuxServer rpm]#vi test.java   class test   {   public static void main(String[] args)   {   System.out.println("Hello World!");   }   }
Save and exit. Next, compile and execute;

[root@LinuxServer text]# javac test.java   [root@LinuxServer text]# JAVA test   Hello World!
Works normally .

6. If you want a user to have the permission to run java commands, just modify its bash initialization file.

For example, if you want to give user longware the permission to run java commands,

[root@LinuxServer root]# vi /home/longware/.bashrc   set JAVA_HOME=/usr/java/j2sdk1.4.2_11   export JAVA_HOME   set PATH=$PATH:$JAVA_HOME/bin   export PATH   set CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar   export CLASSPATH
7. At this point, the installation of JDK on Linux is completed. Next install eclipse-SDK-3.0-linux-gtk.zip and go to www.eclipse.org to download an eclipse3.0. Unzip eclipse-SDK-3.0-linux-gtk.zip to /opt

8, Write a script to start eclipse

#!/bin/bash   #   # 执行 eclipse 3   #   export JAVA_HOME=/usr/java/j2sdk1.4.2_11   export CLASSPATH=/usr/java/j2sdk1.4.2_11/lib   /opt/eclipse/eclipse -vm /usr/java/j2sdk1.4.2_06/bin/java -data /home/chenzhe/wo   rkspace &   # -vm 参数用以指定使用哪一个 jvm 来执行Eclipse,   # -date参数用以指定Eclipse的数据目录。
Specify here that it exists in the workspace directory under the user root directory (/home/chenzhe/)

Save the script to /usr/local/bin , call eclipse, and assign 755 permissions to eclipse

9. Create a launcher on the desktop, select an icon at will, and set the path to /opt/eclipse/eclipse, /opt/eclipse/icon.xpm It is the icon that comes with eclipse.

10. Double-click the icon, or enter eclipse in the terminal, and the Java environment variable configuration is done!##JRVOTE#####

The above is the detailed content of How to configure Java environment variables in Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete