The “java: command not found” error in Bash indicates that the JRE is either not installed or inaccessible. To fix this error, install JRE using:
sudo apt install -y default-jre
In Linux, the java command is used to execute Java programs. It is a command-line utility that launches Java applications by interpreting and running Java bytecode. However, encountering “bash: java: command not found” indicates that either the JDK/JRE is not installed on your system or the PATH variable is not set for Java. Let’s see the reasons for this error in detail and explore the possible fixes.
Reasons for “java: command not found” Error in Bash
Some common reasons for the “java” command not found error are:
- JDK not Installed: If you are unable to use the “java” command, it could be that your system does not have Java Development Kit (JDK).
- Incorrect Path: The PATH on your system may not contain “java”.
- Inadequate Permissions: “java” might not have the authorization it needs to function.
- Binary Not Found: The JRE binary might be missing or corrupted.
3 Methods to Solve “bash: java: command not found” Error
Follow the below methods to solve the java command not found error in Bash:
1. Install JRE Package
Most of the time, the absence of JDK (Java Development Kit) on your system causes the “bash: java: command not found” error. In this instance, APT in Ubuntu, YUM in RedHat, and Pacman in Arch Linux can install it on your system. Java Runtime Environment (JRE) includes JDK and other necessary Java packages. Follow the steps to install JRE in Ubuntu:
- Press CTRL+ALT+T to open the terminal in Ubuntu.
- Update the repository using
sudo apt update
command. - Run the command to install JRE (Java Runtime Environment):
sudo apt install -y default-jre
- Execute
java --version
to verify the installation.
Now, the java command is executed successfully.
2. Set “java” PATH Variable
A prevalent reason behind the “java command not found” is it is not present in your system PATH environment variables. To set the PATH variable for “java”, follow the steps below:
- Press CTRL+ALT+T to open the terminal.
- Find the required details for the Java PATH variables using:
sudo update-alternatives --config java
The output shows the absolute path of Java. It will be used as JAVA_HOME in the /etc/profile file.
- Open the “/etc/profile” file to add the PATH variable:
sudo nano /etc/profile
- Now, paste the below code at the end of the file:
JAVA_HOME=/usr/lib/jvm/java-11-openjdk export JAVA_HOME PATH=$PATH:$JAVA_HOME/bin export PATH
- Press CTRL+S to save, and CTRL+X to exit.
- Run
reboot
to update the PATH variable in the system.
After rebooting the system, you will get rid of the “java: command not found” error.
3. Set an Alias to “java”
To fix the “java: command not found” error, you can create a temporary solution by setting up an alias for java. Here’s how you can do it:
echo "alias java='$(which java)'" >> ~/.bashrc; . ~/.bashrc
This command appends an alias for the java command to the ~/.bashrc file and immediately applies the changes in the current terminal session. The alias ensures that the java command points to the correct path by using the $(which java)
syntax, which dynamically retrieves the actual path to the java executable.
How to Solve “java command not found” Error in MacOS?
If you encounter the “java: command not found” error in MacOS, it likely means that the Java Development Kit (JDK) is not installed or not properly configured on your system. To solve the error, follow the steps below:
- First, open your Mac terminal and run
java --version
to check whether Java is installed or not. If it is installed, this command will display information about the installed JDK version. - If it’s not installed, run the below command to install it:
brew install openjdk
Note: If you don’t have Homebrew installed, you can install it by running the following command in the Terminal:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
-
However, if you get the error even after installing JDK, make sure that the directory containing the java executable is in your system’s PATH. Here’s the command to do it:
export PATH="/usr/local/opt/openjdk/bin:$PATH"
After completing these steps, you should be able to use Java on your macOS system.
Conclusion
In conclusion, resolving the “bash: java: command not found” issue involves ensuring the installation of JRE and configuring the system’s PATH appropriately. Applying these solutions allows you to seamlessly access Java commands within the Bash environment and streamline various aspects of Java application development workflow.
People Also Ask
Why am I getting the `java: command not found` error on Ubuntu?
The “java: command not found” error on Ubuntu suggests that the Java Development Kit (JDK) is either not installed or not included in the system’s PATH. This error typically occurs when the system cannot locate the Java executable, and these steps ensure that the JDK is properly installed and accessible.
How to Fix the ‘Java not recognized’ Error?
The “java not recognized” error typically occurs on Windows when the Java executable is not in the system’s PATH. To fix the error, ensure Java is installed on your system. First, download and install the latest JDK from the official Oracle website. Next, set the JAVA_HOME environment variable to the Java installation directory and add the bin directory within it to your system’s PATH. For example, if Java is in “C:\Program Files\Java\jdk-17”, add “C:\Program Files\Java\jdk-17\bin” to the PATH.
What to do when java command is not working?
When “java“ command is not working, install the default JDK using sudo apt install default-jdk
, verify the installation with java --version
, and ensure that the JDK’s bin directory is in your PATH.
What is the use of java command?
The java
command executes Java programs and applications on a computer. When a Java program is compiled, it is translated into bytecode, which can be executed by the Java Virtual Machine (JVM). It plays a crucial role in running Java applications, applets, and other software developed in Java.
How to Solve “sudo: java: command not found” in Linux?
To resolve “sudo: java: command not found” in Linux, install the default Java Development Kit (JDK) and update the system’s PATH. Use the following commands:
sudo apt update
sudo apt install default-jdk
export PATH="/usr/lib/jvm/default-java/bin:$PATH"
How to verify if Java is installed correctly after fixing the error?
After fixing the “java: command not found” error, you can verify if Java is installed correctly by using the java --version
command. If Java is installed correctly, this command will display information about the installed Java version. Additionally, you can check the Java Compiler version with javac --version
. These commands confirm that Java is accessible in the system’s PATH and running without errors, ensuring a successful installation.
How can I manually set the Java path?
To manually set the Java path, you can update the PATH environment variable in your shell profile file. Open your shell profile file using nano ~/.bashrc
and add the following line:
export PATH="$(which java)/bin:$PATH"
Finally, save the file and either restart your terminal or run source ~/.bashrc
for the changes to take effect.
Related Articles
- [Fixed] “bash: sudo: command not found” Error
- [Solved] Bash “yarn” Command Not Found Error
- [Solved] Bash “docker” Command Not Found Error
- [Fixed] “bash: ng: command not found” Error
- Solved “bash: yum: command not found” [RHEL, Fedora, CentOS]
- [Solved] “bash: vim: command not found” Error
- [Fixed] “bash: curl: command not found” Error
- [Solved] “bash: node: command not found” Error
- [Solved] “bash: code: command not found” Error
- [Fixed!] “systemctl command not found” Error in Linux
<< Go Back to [Solved] “bash: command not found” Error | Bash Error Handling and Debugging | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners