Unlock the Full Potential of Ubuntu: Learn How to Easily Change Your Java Version with These Simple Code Examples

Table of content

  1. Introduction
  2. Understanding Java Versions in Ubuntu
  3. Checking the Current Java Version in Ubuntu
  4. Installing a New Version of Java in Ubuntu
  5. Updating Alternatives to Change the Default Java Version in Ubuntu
  6. Testing Your New Java Version in Ubuntu
  7. Troubleshooting Common Issues
  8. Conclusion



Ubuntu is a popular operating system among developers due to its reliability, security, and flexibility. It comes equipped with various tools and features that make it a popular choice for Android application development.

Java is an essential programming language used for developing Android applications. However, not all versions of Java are compatible with Ubuntu, and it can be challenging to change the Java version needed for development.

This is where these simple code examples come in handy. In this post, you will learn how to quickly change your Java version in Ubuntu using these examples, allowing you to unlock the full potential of Ubuntu for Android application development.

Understanding Java Versions in Ubuntu

Java is a popular programming language used for developing applications across multiple platforms, including Android. To properly run and develop Java-based applications, you need to have the correct version of Java installed on your Ubuntu operating system.

In Ubuntu, multiple versions of Java can be installed simultaneously, allowing you to choose the version that works best for your application. However, it's important to understand the difference between these versions and how to switch between them.

Here are some key terms and concepts to keep in mind when working with Java versions in Ubuntu:

Java Development Kit (JDK)

The JDK is a software development kit used for developing Java applications. It includes the Java Runtime Environment (JRE) and tools needed for compiling, debugging, and documenting Java files.

Java Runtime Environment (JRE)

The JRE is a runtime environment that allows you to run Java applications on your computer. It includes the necessary libraries and components required to execute Java code.

OpenJDK vs Oracle JDK

Ubuntu offers two versions of the JDK: OpenJDK and Oracle JDK. Both versions are functionally similar, but there are some slight differences in licensing and support.

Default Java version

When multiple versions of Java are installed on your Ubuntu system, one version is designated as the default version. This is the version that is used by default when running Java-based applications.

Switching Java versions

To switch to a different version of Java, you can use the update-alternatives command in the terminal. This command allows you to choose which executable should be used for a particular program.

By understanding these concepts and commands, you can unlock the full potential of Ubuntu for Java development. Stay tuned for our next article, where we will provide code examples that demonstrate how to easily change your Java version in Ubuntu.

Checking the Current Java Version in Ubuntu

Before you can change your Java version in Ubuntu, you need to know what version you're currently using. Here's how to check:

  1. Open a terminal window by pressing Ctrl+Alt+T.
  2. Type the following command and press Enter: java -version

You should see output that looks something like this:

openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing)

The first line tells you the version number of your Java installation. In this example, the version is "11.0.6". Note that the exact output may differ depending on how your Java installation is set up.

If you see output that says "Command 'java' not found", it means that Java is not currently installed on your system. You'll need to install it before you can proceed.

Now that you know how to check your current Java version, you're ready to learn how to change it.

Installing a New Version of Java in Ubuntu

In order to use the latest version of Java on Ubuntu, you'll need to install it using the command line. Here are the steps involved:

  1. Open the terminal and check if Java is already installed by typing the following command: java -version. If it is not installed, you'll see an error message.

  2. Add the Ubuntu webupd8team Java PPA (Personal Package Archive) to your system's software sources by typing the following commands in the terminal:

    sudo add-apt-repository -y ppa:webupd8team/java
    sudo apt-get update
  3. Install the latest version of Oracle Java by typing the following command in the terminal:

    sudo apt-get install -y oracle-java8-installer
  4. Once the installation is complete, type the following command to set the default Java version to the newly installed one:

    sudo update-java-alternatives -s java-8-oracle
  5. Verify that the new version of Java is installed and working by typing java -version in the terminal.

By following these simple steps, you can easily install the latest version of Java on your Ubuntu system and unlock its full potential for developing Android applications.

Updating Alternatives to Change the Default Java Version in Ubuntu

When working with Ubuntu, you may encounter a situation where you need to change the default Java version. Fortunately, this can be easily accomplished using the update-alternatives tool. This tool allows you to switch between different versions of Java that are installed on your system. Here's how to do it:

  1. Open your terminal and type the following command to see the available Java versions on your Ubuntu system:
sudo update-alternatives --config java
  1. You will see a list of the installed Java versions. Choose the version that you want to use as the default by typing the number next to it and pressing Enter.

  2. You can also update the alternatives for other Java-related commands, such as javac or jar, using the same format. For example:

sudo update-alternatives --config javac
  1. To confirm that the changes have been made, you can check the default Java version by typing the following command:
java -version

That's it! With just a few simple code examples, you can easily update the default Java version in Ubuntu using the update-alternatives tool. This can be especially useful when working on Android app development projects that require a specific Java version.

Testing Your New Java Version in Ubuntu


Once you have successfully changed your Java version in Ubuntu, it is important to test that your new installation is functioning properly. Here are a few ways to test your new Java version:

  1. Check the Java version: Use the following command to check the Java version of your system:
java -version

If the output of this command matches the version you installed, then your new Java version is working properly. If not, double-check that you installed the correct version and repeat the installation process if needed.

  1. Run a Java program: To test if Java is working properly, try running a Java program. You can use the following command to run the Java program:
java <program_name>

Replace <program_name> with the name of the Java program you want to run. If the program runs without any errors or issues, then your new Java version is functioning properly.

  1. Test Java in your web browser: If you use Java in your web browser, you can test if your new Java version is working by visiting the Java test page on Oracle's website. If the webpage loads and displays correctly, then your new Java version is properly installed and configured.

By testing your new Java version, you can ensure that your system is running smoothly and avoid potential issues down the line.

Troubleshooting Common Issues

While learning how to change your Java version in Ubuntu can greatly enhance your Android development experience, it's not always a smooth process. Here are some common issues you may encounter, along with possible solutions:

1. Java version not recognized

If you have successfully installed a new Java version and set it as your default, but your system still recognizes the old version, try the following steps:

  • Check that the PATH variable is correctly configured in your terminal. You can do this by opening a terminal and typing "echo $PATH". If /usr/bin/java (or another directory containing the old Java version) is listed before the directory containing the new Java version, you'll need to rearrange the directory order in your PATH.

  • Check that the symbolic links in /usr/bin are pointing to the correct Java version. You can check this by running "ls -l /usr/bin/java". If the symbolic link points to the wrong Java version or is broken, you'll need to recreate the link with the correct path.

2. Java installation fails

If you encounter errors while installing Java, these solutions may help:

  • Check that you have downloaded the correct Java package for your system architecture (32-bit vs 64-bit) and version.

  • Check that you have the necessary dependencies installed. You can do this by running "sudo apt-get install -f" before attempting to install Java again.

  • If you're still having trouble, try installing Java from a different source or using a different package manager.

3. Java version incompatible with Android SDK

If you encounter compatibility issues between your Java version and the Android SDK, you may need to install an older Java version. To do this, follow the steps outlined in the main article to install the desired Java version, then use the following commands to switch between versions:

sudo update-alternatives --config javac
sudo update-alternatives --config java

Choose the desired version from the list of installed alternatives.

By understanding and troubleshooting these common issues, you'll be well-equipped to take full advantage of Ubuntu's flexibility and customize your development environment to suit your needs.


Learning how to change your Java version in Ubuntu can be a valuable skill for any developer working with the Android platform. With the help of the examples provided in this guide, you should now have a better understanding of how to install and manage different Java versions using the update-alternatives command.

Remember to check your version before making any changes, and use caution when modifying your system settings. Always test your applications thoroughly after making any updates.

By unlocking the full potential of Ubuntu, you can take advantage of the latest features and capabilities of the Android platform. Whether you are a seasoned developer or just starting out, this guide can be a useful resource for improving your skills and staying up to date with the latest trends and best practices.

Thank you for reading, and happy coding!

Cloud Computing and DevOps Engineering have always been my driving passions, energizing me with enthusiasm and a desire to stay at the forefront of technological innovation. I take great pleasure in innovating and devising workarounds for complex problems. Drawing on over 8 years of professional experience in the IT industry, with a focus on Cloud Computing and DevOps Engineering, I have a track record of success in designing and implementing complex infrastructure projects from diverse perspectives, and devising strategies that have significantly increased revenue. I am currently seeking a challenging position where I can leverage my competencies in a professional manner that maximizes productivity and exceeds expectations.
Posts created 1778

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top