How to install Java on linux (Ubuntu 16.04)

This article is about how to install Java on Linux. In this article we are installing Java8 on Ubuntu 16.04, In similar way you can install on any Linux distribution.


First check JAVA is already installed on your system or not by following command:
If java is properly installed then output is like below:
root@sb:/home/sb# javac

Usage: javac  
where possible options include:
  -g                         Generate all debugging info
  -g:none                    Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info
  -nowarn                    Generate no warnings
  -verbose                   Output messages about what the compiler is doing
  -deprecation               Output source locations where deprecated APIs are used
  -classpath           Specify where to find user class files and annotation processors
  -cp                  Specify where to find user class files and annotation processors
  -sourcepath          Specify where to find input source files
  -bootclasspath       Override location of bootstrap class files
  -extdirs             Override location of installed extensions
  -endorseddirs        Override location of endorsed standards path
  -proc:{none,only}          Control whether annotation processing and/or compilation is done.
  -processor [,,...] Names of the annotation processors to run; bypasses default discovery process
  -processorpath       Specify where to find annotation processors
  -parameters                Generate metadata for reflection on method parameters
  -d              Specify where to place generated class files
  -s              Specify where to place generated source files
  -h              Specify where to place generated native header files
  -implicit:{none,class}     Specify whether or not to generate class files for implicitly referenced files
  -encoding        Specify character encoding used by source files
  -source           Provide source compatibility with specified release
  -target           Generate class files for specific VM version
  -profile          Check that API used is available in the specified profile
  -version                   Version information
  -help                      Print a synopsis of standard options
  -Akey[=value]              Options to pass to annotation processors
  -X                         Print a synopsis of nonstandard options
  -J                   Pass  directly to the runtime system
  -Werror                    Terminate compilation if warnings occur
  @                Read options and filenames from file
Else Install Java
Download the repository for java:
root@root:~$ sudo add-apt-repository ppa:webupd8team/java
Resynchronize the package index files from their sources via Internet:
root@root:~$ sudo apt-get update
Install the java
root@root:~$ sudo apt-get install oracle-java8-installer
Before setting the environment variable we have to identify the path where the java is installed:
root@root:~$ which javac
/usr/bin/javac
root@root:~$ ls -l /usr/bin/javac
/usr/bin/javac -> /etc/alternatives/javac
root@root:~$ ls -l /etc/alternatives/javac
/usr/lib/jvm/java-8-oracle/bin/javac
Path may be different for different Linux version, for ubuntu it's:
/usr/lib/jvm/java-8-oracle/bin/javac
Set the environment variable in .bashrc file:
root@root:~$ gedit ~/.bashrc
Copy paste the following lines to your .bashrc file and save it.
Change java path according to your java path.
For changes in .bashrc file you require root privileges.
export JAVA_HOME=/usr/lib/jvm/java-8-oracle
export PATH=$PATH:$JAVA_HOME/bin
Apply the change in .bashrc file using source command
root@root:~$ source ~/.bashrc
Now check java is installed or not by javac
Now, you can also check JAVA_HOME path On terminal type echo $JAVA_HOME

Great! Java is properly installed on your system.

0 comments:

Post a Comment