Compile and run java program in ubuntu -- configure java environment variables
Configure java environment variables in ubuntu (detailed)
1. decompress and install jdk
Go to the jdk-6u14-linux-i586.bin file directory under the shell terminal,
Execute the command./jdk-6u14-linux-i586.bin at this time there will be a protocol, continue to press enter, when asked whether to agree, enter yes, press Enter. A jdk1.6.0 _ 14 directory will be generated in the current directory. You can copy it to any directory.
2. environment variables to be configured
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.
3. Three Methods for configuring Environment Variables1. 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 at the end of the profile file:
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 on again
· Annotation
A. Change/usr/share/jdk1.6.0 _ 14 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. 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 grant a user permission to use these environment variables, you only need to modify. you can use the bash_profile file.
· Open the. bash_profile file in the user directory in a text editor
· Add the following content to the end of the. bash_profile file:
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 on again
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.6.0 _ 14
Export PATH = $ JAVA_HOME/bin: $ PATH
Export CLASSPATH =.: $ JAVA_HOME/lib/dt. jar: $ JAVA_HOME/lib/tools. jar
4. Test jdk
1. Create a Test. java file in the text editor, enter the following code and save it:
Public class test {
Public static void main (String args []) {
System. out. println (A new jdk test !);
}
}
2. Compile: Execute the command javac Test. java on the shell terminal.
3. Run: run the java Test command on the shell terminal.
When "A new jdk test!" appears in shell !" Jdk runs normally.
5. Uninstall jdk
· Find the _ uninst subdirectory of the jdk installation directory.
· Run the command./uninstall. sh on the shell terminal to uninstall jdk. Desire has no rest.