Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

How to run the correct Java version after an update

January 9, 2023
Mohammadi Iram
Related topics:
Java
Related products:
Red Hat Enterprise Linux

Share:

    When you try to run a Java program after updating your system's version of Java, you are likely to receive an error message such as the following:

    mvn install
    The JAVA_HOME environment variable is not defined correctly
    This environment variable is needed to run this program
    NB: JAVA_HOME should point to a JDK not a JRE
    

    The source of the problem is probably the setting of your JAVA_HOME environment variable. This article shows you how to fix this problem.

    The reason for the error message

    Java programs need a Java Runtime Environment (JRE) to run. When you program with Java, the JRE is part of the Java Development Kit (JDK). The meanings of these terms follow:

    • Java Runtime Environment (JRE): Java source code is compiled and converted to Java bytecode. If you want to run this bytecode on your platform, you need the JRE to load and interpret the bytecode.
    • Java Development Kit (JDK): This includes a JRE along with a collection of software development tools and libraries to create Java programs that can run on the platform using the JRE.

    The importance of JAVA_HOME

    Your shell finds the proper Java environment through an environment variable called JAVA_HOME. It could point to either a JDK or a JRE. As the error message shown earlier indicates, you should point to your JDK if you have a JDK. When non-developers run Java, they don't need the entire JDK, so they can download a JRE and point JAVA_HOME to that. You can learn more about the Java platform components in the article, What is the Java Runtime Environment.

    When you upgrade Java, the system installs a new JDK, and you need to update JAVA_HOME to point to the new location.

    Environment variables used in the shell are generally defined in a start-up file. This file is located in your home directory and is called .bashrc if you use the Bash shell that is the default on Linux. Note the initial dot in the name .bashrc: this dot means that the file doesn't normally turn up when you display the contents of the directory.

    The .bashrc file is a script that runs when a user logs in. The file itself contains a series of commands to configure the terminal session.

    Check your .bashrc file to see whether it defines JAVA_HOME and what value is assigned to the environment variable. For instance, you can execute this command in the terminal:

    $ more ~/.bashrc

    The line defining JAVA_HOME in the output is as follows:

    export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-17.0.4.1.1–1.fc35.x86_64

    You have to make sure that JAVA_HOME is set correctly, with version details.

    Choosing a JDK

    In the terminal on a Linux system, run the following command:

    $ sudo alternatives --config java

    The output displays the versions of Java currently on the system, as shown in Figure 1. The command also issues a prompt allowing you to choose a version.

    Choose the JDK you want.
    Figure 1: You can choose which JDK you want.

    The version you choose (3 in the figure) contains, within parentheses, a full pathname that you have to assign to your JAVA_HOME environment variable in your .bashrc file. After entering the number that you want to use, press the Enter key to finish the command.

    Updating .bashrc

    The next step is to check the value of JAVA_HOME (or add it, if it is not already present) in the .bashrc file. If .bashrc doesn't match the pathname you choose, you can copy the pathname from the terminal display and paste it into .bashrc as I'll show shortly.

    Figure 2 highlights the JDK path I selected in Figure 1.

    Shows the JDK selected: openjdk-17.0.5.0.8–2.fc35.x86_64.
    Figure 2: The white highlight shows the JDK we have selected on our system, openjdk-17.0.5.0.8–2.fc35.x86_64.

     

    Figure 3 highlights the JDK path in the JAVA_HOME setting in my current .bashrc file.

    Shows an old JDK: openjdk-17.0.4.1.1–1.fc35.x86_64.
    Figure 3: The .bashrc file expects an old JDK, openjdk-17.0.4.1.1–1.fc35.x86_64.

    The paths are different, so I need to update the environment variable in the .bashrc file to match the path offered by the system. Specifically, I need to change the JDK version in the path from openjdk-17.0.4.1.1–1.fc35.x86_64 to openjdk-17.0.5.0.8–2.fc35.x86_64.

    When you save your changes, .bashrc looks like Figure 4.

    Shows the updated .bashrc file.
    Figure 4: The .bashrc file is updated now to expect openjdk-17.0.5.0.8–2.fc35.x86_64.

    Updating the shell

    To reflect the changes in your shell, either close and launch the terminal again or use the following command:

    $ source ~/.bashrc

    In this way, you can easily update your JAVA_HOME environment variable and continue coding in Java.

    Related Posts

    • Install OpenJDK on Windows and Linux

    • Checkpointing Java from outside of Java

    • Introducing the Red Hat build of the OpenJDK Universal Base Images—now in Red Hat Enterprise Linux 8.2

    Recent Posts

    • Alternatives to creating bootc images from scratch

    • How to update OpenStack Services on OpenShift

    • How to integrate vLLM inference into your macOS and iOS apps

    • How Insights events enhance system life cycle management

    • Meet the Red Hat Node.js team at PowerUP 2025

    What’s up next?

    The microservice architectural approach is more than just about technology: It reaches into the foundation of your organization to allow you to build truly scalable, adaptive, complex systems that help a business adapt to rapidly changing competitive markets. In Microservices for Java Developers, you'll get a hands-on introduction to frameworks and containers through a handful of familiar patterns.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue