There are several JDK implementations available for Linux, such as
Oracle JDK, OpenJDK, Sun JDK, IBM JDK and GNU Java Compiler. We shall
choose the Oracle JDK 8. Ubuntu chooses OpenJDK as its default JDK,
which is not 100% compatible with Oracle JDK.
To remove OpenJDK, issue command:
Step 0: Check if JDK has already been Installed
Open a Terminal and issue this command:$ javac -versionIf a JDK version number (e.g., "
javac 1.x.x_xx
") appears, JDK has already been installed. You can skip the installation and goto step 2.To remove OpenJDK, issue command:
$ sudo apt-get purge openjdk-\*
Step 1: Download and Install JDK
- Goto JDK (Java SE) download site @ http://www.oracle.com/technetwork/java/javase/downloads/index.html.
Select "Java SE 8u{xx}" ⇒ JDK ⇒ Download ⇒ "Accept License Agreement" ⇒
Select Linux x86 (for 32-bit system) or Linux x64 (for 64-bit system) "
tar.gz
" package, e.g., "jdk-8u{xx}-linux-i586.tar.gz
". (To check your OS version, goto "Settings" ⇒ "Details"; or issue command "file /sbin/init
".) The tarball will be stored in folder "~/Downloads
", by default. - We shall install JDK under "
/usr/local/java
" (or Ubuntu's default JDK directory/usr/lib/jvm
). First, create a directory "java
" under "/usr/local
". Open a Terminal and issue these commands:$ cd /usr/local $ sudo mkdir java
Extract the downloaded package (Check your downloaded filename!)$ cd /usr/local/java $ sudo tar xzvf ~/Downloads/jdk-8u{xx}-linux-x64.tar.gz // x: extract, z: for unzipping gz, v: verbose, f: filename
JDK shall be extracted in a folder "/usr/local/java/jdk1.8.0_{xx}
", where{xx}
is the upgrade number. - Inform the Ubuntu to use this JDK/JRE:
// Setup the location of java, javac and javaws $ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk1.8.0_{xx}/jre/bin/java" 1 // --install symlink name path priority $ sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.8.0_{xx}/bin/javac" 1 $ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jdk1.8.0_{xx}/jre/bin/javaws" 1 // Use this Oracle JDK/JRE as the default $ sudo update-alternatives --set java /usr/local/java/jdk1.8.0_{xx}/jre/bin/java // --set name path $ sudo update-alternatives --set javac /usr/local/java/jdk1.8.0_{xx}/bin/javac $ sudo update-alternatives --set javaws /usr/local/java/jdk1.8.0_{xx}/jre/bin/javaws
The above steps set up symlinksjava
,javac
,javaws
at/usr/bin
(which is in the PATH), that link to/etc/alternatives
and then to JDK bin directory.
The "alternatives" system aims to resolve the situation where several programs fulfilling the same function (e.g., different version of JDKs). It sets up symlinks thru/etc/alternatives
to refer to the actual programs to be used.
$ cd /usr/bin $ ls -ld java* lrwxrwxrwx 1 root root 22 Mar 31 20:41 java -> /etc/alternatives/java lrwxrwxrwx 1 root root 23 Mar 31 20:42 javac -> /etc/alternatives/javac lrwxrwxrwx 1 root root 24 Mar 31 20:42 javaws -> /etc/alternatives/javaws $ cd /etc/alternatives $ ls -ld java* lrwxrwxrwx 1 root root 40 Aug 29 18:18 java -> /usr/local/java/jdk1.8.0_20/jre/bin/java lrwxrwxrwx 1 root root 37 Aug 29 18:18 javac -> /usr/local/java/jdk1.8.0_20/bin/javac lrwxrwxrwx 1 root root 42 Aug 29 18:19 javaws -> /usr/local/java/jdk1.8.0_20/jre/bin/javaws
- To verify the JDK installation, issue these commands:
// Show the Java Compiler (javac) version $ javac -version javac 1.8.0_20 // Show the Java Runtime (java) version $ java -version java version "1.8.0_20" Java(TM) SE Runtime Environment (build 1.8.0_20-b26) Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode) // Show the location of javac and java $ which javac /usr/bin/javac $ which java /usr/bin/java
- To use Java under Firefox, you need to enable the so-called "Java Plugin for web browser".
$ cd /usr/lib/mozilla/plugins // if this directory does not exist, create it $ sudo mkdir -p /usr/lib/mozilla/plugins
Then, create a symbolic link to your Mozilla plugins folder, (check your JDK folder)$ cd /usr/lib/mozilla/plugins $ sudo ln -s /usr/local/java/jdk1.8.0_{xx}/jre/lib/amd64/libnpjp2.so
To verify the installation, restart your Firefox and issue URL "about:plugins
".Check for Java plugins with the correct version.
Starting from JDK 1.8u??, to run unsigned applets, you need to set security level to "high" add the sites to the "Exception List" (under the Java Control Panel ⇒ Security). To start the Java Control Panel:$ cd /usr/local/java/jdk1.8.0_{xx}/jre/bin $ ./ControlPanel // OR ./jcontrol
You need to restart Firefox after modifying the Exception List. - [Don't Do this step - taken care by "alternative" in Step 3. Keep
here to show you how to set PATH.] Add JDK's binary directory ("
bin
") to the "PATH" by editing "/etc/profile
":$ cd /etc $ gksudo gedit profile // OR "sudo nano profile" to use the console-based nano editor
Add these lines at the end of the file "/etc/profile
", replace "{xx}
" with the actual number:export JAVA_HOME=/usr/local/java/jdk1.8.0_{xx} export PATH=$JAVA_HOME/bin:$PATH
Rerun the configuration file by:$ source /etc/profile // Check the new settings for JAVA_HOME and PATH $ echo $JAVA_HOME /usr/local/java/jdk1.8.0_{xx} $ echo $PATH .....:/usr/local/java/jdk1.8.0_{xx}/bin
Step 2: Compile and Run a Hello-world Java Program
- Open "Folder" and create a new directory called "
myProject
" under your home directory to keep all your works. - Open "Text Editor" (gedit). Enter the following source code and save as "
Hello.java
" under the "~/myProject
" directory created earlier.public class Hello { // To save as "Hello.java" under "~/myProject" public static void main(String[] args) { System.out.println("Hello, world from Ubuntu!"); } }
- To compile the Hello-world Java program, launch a Terminal and issue these commands:
// Change directory to where the source code resides $ cd ~/myProject // List the contents of current directory. Check for "Hello.java" $ ls ...... Hello.java .... // Compile "Hello.java" into "Hello.class" $ javac Hello.java // Check for "Hello.class" $ ls ...... Hello.class ....
- Run the Hello-world Java program:
// Run "Hello.class" $ java Hello Hello, world from Ubuntu!
No comments:
Post a Comment