Determine if the installation
First, we have to determine whether the machine is installed JDK, many people recommend the use of the Java-version command. Using the java-version command on my computer, the contents are as follows:
Java Version "1.7.0_45"
OpenJDK Runtime Environment (rhel-2.4.3.2.el6_4-i386 u45-b15)
OpenJDK Server VM (build 24.45-b08, Mixed mode)
It looks as if the JDK is already installed in my machine, but when I use Javac, I get a hint:
-bash:javac:command not found
This means that the first Java command, not the Java command in the installed JDK, has a Java file that can be executed under all the paths of the $path, stating that the JDK is still not installed properly. Pay special attention to this.
Installing the JDK
- Go to http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html to download the installation files for the JDK. Since my Linux is 32 bits, I download the jdk-8u25-linux-i586.tar.gz file.
- Create a new/usr/java folder, place jdk-8u25-linux-i586.tar.gz in the folder, and switch the working directory to the/usr/java directory.
- Execute the command TAR-ZXVF jdk-8u25-linux-i586.gz decompression, decompression found/usr/java more than a jdk1.8.0_25 folder.
- With the above steps, the JDK is fully installed. The following is the configuration of the environment variable.
Configuring Environment variables
- Edit a profile using Vim/etc/profile
- At the bottom of the/etc/profile, add the following:
Java_home=/usr/java/jdk1.8.0_25
Path= $JAVA _home/bin: $PATH
Classpath= $JAVA _home/jre/lib/ext: $JAVA _home/lib/tools.jar
Export PATH java_home CLASSPATH
- Above, the environment variable configuration is complete. Note that the path in the configuration, it is necessary to put $java_home/bin in front , or use the Java command, the system will find the previous Java, do not look down. In this way, the JAVA executable file run directory is not $java_home/bin, and in other directories, will cause a lot of problems.
- Also note that the previous tutorial written classpath= $JAVA _home/lib.tools.jar, do not know what the previous version is, the current version is not such a jar package .
- Finally, use Source/etc/profile to make the profile effective immediately.
Command test
- Command not found error is not present with JAVAC commands
- Using Java-version, the version "1.8.0_25" appears in Java edition
- echo $JAVA _home, Echo $CLASSPATH, echo $PATH to see if your configuration is correct.
Code testing
Create a new file in your own working directory Hello.java, write the following:
Class hello{ void main (string[] args) {System.out.println ("Hello World");} }
Execute the command below and if you get the following result, the JDK installation is complete.
[[email protected] ~/javaprojects/test]$ javac hello.java[[email protected] ~/javaprojects/test]$ java-CP . Hellohello World
Of course, we installed the JDK8, should test the characteristics of the JDK8. You can use the JDK8-specific stream to test the code as follows:
ImportJava.util.Arrays;ImportJava.util.Iterator;ImportJava.util.List;ImportJava.util.function.Consumer;import Java.util.function.function;import Java.util.function.predicate; Public class TestLambda1 { public static void< Span style= "color: #000000;" > main (string[] args) {// Aggregate Operations list< string> myList = arrays.aslist ("Zhangsan", "Lisi", "Wangwu" ); Mylist.stream (). Filter (X-X.contains ("a")). Map (x-> X.touppercase ()). ForEach (x-> System.out.println (x))}}
Run with the following command to get the results, stating that there is no problem with the installation.
[Email protected] ~/javaprojects/test]$ javac Testlambda.java [[email protected] ~/javaprojects/test]$ Java- CP . Testlambdazhangsanwangwu
How to install JDK8 under CentOS