How to Install Java on Linux

Java allows you run cross-platform applications that can run on Mac OS-X, Linux, and Windows (among other OSs) without modification. Here’s how to install it on a GNU/Linux machine.

Steps

Manual Non-RPM Method

This is the generic method that also works with GNU/Linux clones that do not support RPM. It does not require administrator rights and allows installing multiple Java versions on the same computer.

  1. 1.Download the JDK.[1].
    • Click on the “Download” link under Java Platform (JDK) 7u4.
    • Accept the license and continue.
  2. 2.Select the right download. Under the “Product/File Description” you should pick your corresponding Linux option. For example, if you’re running Linux x86 (32-bit), you need to choose the corresponding “*.tar.gz” version.

    2943-21

  3. 3.Download the file as a .gz. Save it to your GNU/Linux machine.
  4. 4.Switch to the directory where you saved the file. You do not need to be a root and only must have the write access to the folder where you wish to install Java. If your administrator is not supportive, you may need to place Java into your home folder or (even better) on some shared network location
  5. 5.Uncompress the file. For example, you could do this in your home folder. Uncompressing will create a folder called “jdk1.7.0_04”. Java is now installed.
    • The installed Java jre is rather independent and can be easily moved into another place just by copying all its files.
    • You can install multiple different jre’s this way: they coexist together and can be used if some software requires the older version to run.
  6. 6.Launch Java. The Java executable you need to launch is located in a subfolder, called “bin.” This way of installation will not configure a default Java command for you — you must do this manually or always include the full path in your startup script.

Manual RPM Method

This seems like a “more civilized” way to install Java because it allows the installer to check the dependencies on some system libraries that may be missing. However, it does not support versioning easily and may fail even in some systems that do support RPMs. (Though the current Java installations are rather self-dependent and the required minimal requirements are usually satisfied anyway.)

  1. 1.Download the JDK.[2].
    • Click on the “Download” link under Java Platform (JDK) 7u4.
    • Accept the license and continue.
  2. 2.Select the right download. Under the “Product/File Description” you should pick your corresponding Linux option. For example, if you’re running Linux x86 (32-bit), you need to choose the corresponding “*.rpm” version, for example “jdk-7u4-linux-i586.rpm”.

    2943-2

  3. 3.Download the .rpm file. Save it to your GNU/Linux machine.
  4. 4.Log in as root and switch to the directory where you saved the file. Or, become root by running su and entering the super-user password.
  5. 5.Install the rpm file by executing ‘rpm -ivh filename.rpm’, where filename is the name of your .rpm file. (Such as jdk-7u4-linux-i586.rpm).
    • You may receive a message telling that program “rpm” is not installed.
    • If that is the case the program “rpm” is not installed. You need to install it writing: sudo apt-get install rpm. Enter your password, and you’re finished.
  6. 6.Create symbolic links. If you want to be able to execute this version of Java interpretor or compiler from any directory on your GNU/Linux system, you will have to create a few symbolic links:
    • sudo ln -s -v jdk1.7.0_04/bin/java /usr/bin/java
    • sudo ln -s -v jdk1.7.0_04/bin/javac /usr/bin/javac
    • Note that by installing using this method there could be dependencies that fail. It is better to use the package installer that handles all dependencies for you, since you will not be able to use Java correctly until the dependencies are resolved.

Ubuntu Method Using a GUI Package Manager

  1. 1.Open a package manager. You can use Synaptic or Adept Manager.
  2. 2.Install Open JDK. Java JDK and JRE are not available for installation through GUI Package Manager. You’ll need to install Open JDK instead.
    • Perform a search for openjdk-7-jdk.
    • Select openjdk-7-jdk for installation. Depending on the package manager, you may be asked if you wish to install the required dependencies or it will automatically select them without confirmation. For example, the openjdk-7-jre was already selected by Synaptic Package Manager. If you want to use Java as a plug-in in your browser, then also select to install icedtea-7-plugin.
  3. 3.Apply the changes. Click the button to apply the changes. Depending on the package manager, a pop-up window may appear asking for you to confirm the changes.
  4. 4.Wait for Java to install.

Ubuntu Method Using a Console

  1. 1.Enter one of the following commands into your console program:
    • sudo apt-get install openjdk-7-jdk openjdk-7-jre (if you don’t want the browser plug-in)
    • sudo apt-get install openjdk-7-jdk openjdk-7-jre icedtea-7-plugin (if you do need the plug-in)
    • apt-get automatically takes care of the dependencies and lists the changes for confirmation.
  2. 2.Enter y to confirm the installation.

Ubuntu OpenJDK Method Using a Console

  1. 1.Ubuntu no longer supports the Sun Java package in favor of OpenJDK.
  2. 2.Enter one of the following into your console program:
    • If you do not want the browser plugin, sudo apt-get install openjdk-6-jre.
    • If you do want the browser plugin, sudo apt-get install openjdk-6-jre icedtea6-plugin
    • If you’re working on a server without graphics, and want an environment just for running server applications (e.e. Tomcat or Glassfish), sudo apt-get install –no-install-recommends openjdk-6-jre-headless
    • If you need the full JDK (for writing Java programs): sudo apt-get install openjdk-6-jdk
    • apt-get automatically takes care of the dependencies and lists the changes for confirmation.
  3. 3.Enter y to confirm the installation.

Tips

  • It’s much easier to install from the repositories than from the download on Oracle’s site.
  • Even though it might be unfamiliar to you, using the console method is actually easier than the GUI method.
  • If you find how-to specific for your distribution we recommend using it because in some distributions, Java can be included in the software repositories (which you can benefit from).
  • On Red Hat 8.0 Linux, one could start a Nautilus file manager, desending to the directory, and click on the name of the rpm filename and activate the installation process. This does not work any more in Fedora Core 4 (other FC versions not tested). In reality, if you do this you will get a (warning) message saying that the file name indicates the file is not executable while the content of the file is of the type “executable”.
  • Another tip to follow progress of the rpm install is to use ‘rpm -ivv … ‘ which turns on theverbose output and gives you more info on the install as it progresses. You can do the same thing with other rpm actions to get rpm to be more verbose about what is going on.

Sources and Citations

  1.  www.oracle.com/technetwork/java/javase/downloads/index.html
  2.  www.oracle.com/technetwork/java/javase/downloads/index.html