How to fix the "Java command not found" error in Mac, Ubuntu or Windows 10

If you trying to set up Java in your system or it's already installed but you are getting a "Java command not found" error. It may be due to either it's not installed properly or the path variable is not set up correctly for your operating system. In this tutorial, I will let you know the possible cause of this Java error and how to fix it easily.

Below mentioned are the common Java-related error you may encounter -

  • java command not found ubuntu
  • java command not found windows 10
  • -bash: java: command not found mac
  • Java is not recognized as an internal or external command?
  • java command not found Red Hat
  • java environment not found

Why am I getting a Java command not found error?

Generally, you get a java command not found error due to the following reasons -

  1. Java is not installed in your system
  2. Java installation is not complete due to errors during installation
  3. Environment variable or Path is not setup correctly for Java
  4. Java conflict due to more than one java installation in your system

How do I fix the Java command not found?

These are the quick checks before starting troubleshooting to fix the java command not found error -

  • Check whether java is installed or not?
  • If java is installed, look for the java version
  • Validate environment variable or path is set up correctly for your operating system
  • Make sure you are using the right version in case of more than one Java version installed

Let's deep dive and see step-by-step solutions -

Solution-1 - Install the Java correctly in Windows to get rid of the Java command not found error

Mostly, you don't have java installed and installing java is the first step to fix java related issues. Follow these steps based on your operating system to install java correctly.

Step1 - Check whether Java is installed in your system

  • In Windows, Open the command prompt and run
java --version
  • In the case of Linux, run
sudo java -version

output -
bash: java: command not found

In case you get "Java command not found" or Java is not recognized as an internal or external command then keep following this tutorial to install java in your system.

java-command not found error in Windows 10

sudo update-alternatives --config java

How to install Java in Windows 10

I would recommend you to download the latest java and install it in your system.

Step2 Download Java

Visit oracle.com and download JDK 18 (JAVA SE Development Kit) or the latest available for your system. I am downloading it for Windows 10 (64-bit). Soon Java 19 will supersede JDK 18.

Download latest java 18 for Windows 10

Step3 Install the latest Java 18 on Windows 10/11

Once you have downloaded the "jdk-18_windows-x64_bin.exe" for your Windows system. Double click and run the installer and follow the steps

  • I recommend keeping "C:\Program files\java\jdk-18.0.1.1 (64-bit)" as the default installation folder. Select Next.
Java 18 default installation path in Windows 10

You may select different installation folders also. For example, if you are installing a second instance of java software. Click on change and browse for the target folder.

Solution2 - How to set an environment variable for java in Windows 10, if you have Java already installed

If you have Java already installed and then all the environment variables are set up automatically as shown in the image.

set up Java-Path in Windows 10

But in case you are still getting "Java is not recognised as an internal or external command" error then set up the environment variable for your Java.

  • Type "environment" in the search window. Click "open" on "Edit the system environment variables"
environment-variable-windows10
  • Click on Environment variables.
java-runtime-environment-variable
  • Select "Path" > Then click Edit.
How-to-set-environment-variable-for-java
  • Select New > Give path of java installation directory > For example, "C:\Program files\java\jdk-18.0.1.1\bin" in my case. If you have selected the custom folder to install Java. You can enter your path to the bin folder. It will set up only the java variable for that specific user.

Click OK.

add Java Home in Windows 10 for Java 18
  • Set "classpath" for java. Click "New" in the environment variable window. It allows to set up the new environment variables.
How-to-set-classpath-variable-for-java

In "Variable name", enter "classpath". Also in "variable value" provide path to your java installation till "lib" folder. In my case it is "C:\program files\java\jdk-18.0.1.1\lib > Select OK

set up Java 18 lib folder path
  • Close all open environment variables windows by selecting OK.

Solution3- How to install Java in Ubuntu 21.04 correctly

To install Java in Linux -

  • Visit oracle.com and download JDK 18 or the latest available .deb and transfer it to your system. I am downloading Debian package jdk-18_linux-x64_bin.deb for Ubuntu 21.04. The same can be used for any Debian-based OS like Linux Mint, Kubuntu, Lubuntu etc.
Download latest java 18 for Ubuntu or RedHat Linux
  • Rename the .deb file using the mv command, if required. Then install the Debian package using the dpkg command.

Learn more ways to install the .deb package.

$ sudo dpkg -i ".deb-file-path"
Install Java 18 in Ubuntu 21 to fix java command not found error

For example

$ sudo dpkg -i ./jdk-18_linux-x64_bin.deb

In case you get any error related to the libc6-x32 dependency package, then

dpkg: dependency problems prevent configuration of jdk-18:
jdk-18 depends on libc6-i386; however:
Package libc6-i386 is not installed.
jdk-18 depends on libc6-x32; however:
Package libc6-x32 is not installed.

Either install the dependency package or run the following command to fix the broken.

$ sudo apt --fix-broken install

Once all the dependent packages are installed, try installing java 18 again on Ubuntu 21.

$ sudo dpkg -i ./jdk-18_linux-x64_bin.deb
  • You can use the rpm command to install java in RPM-based operating systems
$ sudo rpm  -ivh "path to rpm"

For example

$ sudo rpm -ivh jdk-18_linux-x64_bin.rpm

Solution4 - How to set the "JAVA_HOME" variable in Ubuntu 21.04

Set the JAVA_HOME variable for all users in Ubuntu by editing the environment file.

$ sudo vi /etc/environment
  • Add path "/usr/lib/jvm/jdk-18/bin" to the last of the environment file as shown in the image. Then, save and exit

The output of the Environment file after adding the Java path

set up java bin directory for Java home in Ubuntu
  • Run source command to execute changes made in the environment file.
$ sudo source /etc/environment
  • Check your java version
$ sudo java -version
Check java version 18 in Ubuntu 21

Solution5- How to install Java in macOS to fix the java command not found error

To install the JDK on macOS:

  1. Download the JDK .dmg file for macOS, jdk-18_macos-x64_bin.dmg from the Java SE Downloads page.
  2. Then Click Accept License Agreement.
  3. Double-click the .dmg file to start it
  4. Finder window appears that contains an icon of an open box and the name of the .pkg file
  5. Double-click the JDK 18.pkg icon to start the installation application
  6. The installation application displays the Introduction window
  7. Click Continue. The Installation Type window appears.
  8. Click Install. A window appears that displays the message: Installer is trying to install new software. Enter your password to allow this.
  9. Enter the Administrator user name and password and click Install Software. The software is installed and a confirmation window is displayed.

You can delete the .dmg file if you want to save disk space after installation.

Solution6 - How to set Java_Home environment variable in macOS

  • If you use the bash shell, run
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
  • In the case of zsh shell, run
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc
  • Restart your shell once in both cases

Solution-7 Uninstall and reinstall Java, if nothing works

If you are getting java command not found error even after setting up java home variable. It means there is something wrong with the installation. In that case, my recommendation will be to uninstall all existing versions of java and reinstall it.

Check out my other post, If you don't know how to uninstall java from Windows or Linux. There are individual sections provided for the uninstallation of Java.

Summary

I am sure, you will be able to fix your java command not found error by following this tutorial step by step and carefully.

Let me know, in case you still face any issues. I will try to help you out to the best of my knowledge. You can check out these Oracle tutorials to learn java after installation by following this post step by step.

Leave a Comment

Your email address will not be published.