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
Table of Contents
- 1 Why am I getting a Java command not found error?
- 2 How do I fix the Java command not found?
- 3 Solution-1 - Install the Java correctly in Windows to get rid of the Java command not found error
- 4 How to install Java in Windows 10
- 5 Solution2 - How to set an environment variable for java in Windows 10, if you have Java already installed
- 6 Solution3- How to install Java in Ubuntu 21.04 correctly
- 7 Solution4 - How to set the "JAVA_HOME" variable in Ubuntu 21.04
- 8 Solution5- How to install Java in macOS to fix the java command not found error
- 9 Solution6 - How to set Java_Home environment variable in macOS
- 10 Solution-7 Uninstall and reinstall Java, if nothing works
- 11 Summary
Why am I getting a Java command not found error?
Generally, you get a java command not found error due to the following reasons -
- Java is not installed in your system
- Java installation is not complete due to errors during installation
- Environment variable or Path is not setup correctly for Java
- 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
- 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.
- To check default versions installed in Linux, run
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.
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-188.8.131.52 (64-bit)" as the default installation folder. Select Next.
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.
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"
- Click on Environment variables.
- Select "Path" > Then click Edit.
- Select New > Give path of java installation directory > For example, "C:\Program files\java\jdk-184.108.40.206\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.
- Set "classpath" for java. Click "New" in the environment variable window. It allows to set up the new environment variables.
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-220.127.116.11\lib > Select OK
- 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.
- 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"
$ 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"
$ 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.
- Edit /system/environment file using vi or nano editor.
$ 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
- Run source command to execute changes made in the environment file.
$ sudo source /etc/environment
- Check your java version
$ sudo java -version
Solution5- How to install Java in macOS to fix the java command not found error
To install the JDK on macOS:
- Download the JDK
.dmgfile for macOS,
jdk-18_macos-x64_bin.dmgfrom the Java SE Downloads page.
- Then Click Accept License Agreement.
- Double-click the
.dmgfile to start it
- A Finder window appears that contains an icon of an open box and the name of the
- Double-click the
JDK 18.pkgicon to start the installation application
- The installation application displays the Introduction window
- Click Continue. The Installation Type window appears.
- Click Install. A window appears that displays the message: Installer is trying to install new software. Enter your password to allow this.
- 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.
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.