If you’ve recently installed Java and encounter the following error while compiling a program:
‘javac’ is not recognized as an internal or external command,
operable program or batch file.
don’t worry—this is one of the most common Java installation and configuration errors, especially for beginners. The error usually indicates that the Java compiler (javac) cannot be found because the Java Development Kit (JDK) is not installed correctly or the PATH environment variable is not configured properly.
In this guide, you’ll learn what this error means, why it occurs, and how to fix it step by step on Windows, Linux, and macOS.
What is javac?
javac is the Java compiler provided with the Java Development Kit (JDK). It converts Java source code (.java files) into Java bytecode (.class files), which can then be executed by the Java Virtual Machine (JVM).
For example:
javac HelloWorld.java
This command compiles:
HelloWorld.java
into:
HelloWorld.class
Without javac, you cannot compile Java programs.
What Does “javac Is Not Recognized” Mean?
When you execute:
javac HelloWorld.java
Windows searches for the javac.exe executable in the directories listed in the PATH environment variable.
If it cannot find javac.exe, it displays:
‘javac’ is not recognized as an internal or external command,
operable program or batch file.
This means the Java compiler is either:
- Not installed
- Not accessible through the PATH variable
- Installed incorrectly
Common Causes of the Error
The most common reasons include:
- JDK is not installed
- Only JRE is installed
- PATH variable is missing
- JAVA_HOME is not configured
- PATH points to the wrong directory
- Multiple Java versions are installed
- Incorrect JDK installation
- Terminal needs to be restarted
Let’s solve each issue step by step.
Solution 1: Verify Java Installation
First, check whether Java is installed.
Run:
java -version
Then check the compiler:
javac -version
Possible result:
'javac' is not recognized...
If java works but javac does not, you likely have only the Java Runtime Environment (JRE) installed.
Solution 2: Install the Java Development Kit (JDK)
The javac compiler is included only with the JDK, not the JRE.
After installing the JDK, verify:
javac -version
Expected output:
javac 21
or
javac 17.0.10
If the command succeeds, the compiler is installed correctly.
Solution 3: Locate the JDK Installation Directory
Find where the JDK is installed.
Windows
Common locations:
C:\Program Files\Java\jdk-21
C:\Program Files\Java\jdk-17
The compiler is located in:
C:\Program Files\Java\jdk-21\bin
Verify that the following file exists:
javac.exe
Solution 4: Configure JAVA_HOME
Create the JAVA_HOME environment variable.
Variable Name:
JAVA_HOME
Variable Value:
C:\Program Files\Java\jdk-21
Do not include the bin folder.
Incorrect:
C:\Program Files\Java\jdk-21\bin
Correct:
C:\Program Files\Java\jdk-21
Solution 5: Update the PATH Variable
Add the following entry to the PATH variable:
Windows:
%JAVA_HOME%\bin
Or directly:
C:\Program Files\Java\jdk-21\bin
After updating PATH:
- Save the changes.
- Close Command Prompt.
- Open a new Command Prompt.
Test:
javac -version
Solution 6: Verify the PATH Variable
To view the PATH:
Windows
echo %PATH%
Ensure the JDK bin directory appears in the output.
Example:
C:\Program Files\Java\jdk-21\bin
If it is missing, Java cannot locate the compiler.
Solution 7: Verify JAVA_HOME
Check:
Windows:
echo %JAVA_HOME%
Expected:
C:\Program Files\Java\jdk-21
Linux/macOS:
If nothing is displayed, configure the variable before continuing.
Solution 8: Restart the Terminal
Environment variable changes do not affect already-open terminals.
After configuring PATH:
- Close Command Prompt
- Open a new Command Prompt
- Run:
javac -version
Sometimes a full system restart is required, especially on Windows
echo $JAVA_HOME
Solution 9: Check for Multiple Java Versions
Many developers have multiple JDKs installed.
Example:
jdk-8
jdk-11
jdk-17
jdk-21
Verify:
java -version
and
javac -version
Both commands should report compatible versions.
If not, update JAVA_HOME and PATH to use the intended JDK.
Solution 10: Configure IDE Settings
Integrated Development Environments (IDEs) may use their own JDK configuration.
IntelliJ IDEA
- File → Project Structure
- Project SDK
- Select the correct JDK
Eclipse
- Preferences
- Java
- Installed JREs
- Add or select the installed JDK
VS Code
- Configure the Java Runtime in the Java Extension settings.
- Ensure the workspace uses the correct JDK.
Solution 11: Configure Linux and macOS
Linux:
Edit:
nano ~/.bashrc
Add:
export JAVA_HOME=/usr/lib/jvm/jdk-21
export PATH=$JAVA_HOME/bin:$PATH
Reload:
source ~/.bashrc
macOS:
Edit:
nano ~/.zshrc
Add:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
Reload:
source ~/.zshrcSolution \
Verify:
javac -version
Solution 12: Reinstall the JDK
If all else fails:
- Uninstall Java.
- Delete outdated JDK directories (if appropriate).
- Install the latest JDK.
- Configure JAVA_HOME.
- Update PATH.
- Restart the terminal.
A clean installation often resolves corrupted or incomplete configurations.
Real-World Example
Suppose you create:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Compile:
javac HelloWorld.java
Output:
After configuring:
JAVA_HOME=C:\Program Files\Java\jdk-21
and adding:
%JAVA_HOME%\bin
to PATH, restart Command Prompt.
Run:
javac HelloWorld.java
Run:
java HelloWorld
Common Scenarios That Cause “javac Is Not Recognized”
| Scenario | Solution |
|---|---|
| Only JRE installed | Install the JDK |
| JDK not installed | Install the latest JDK |
| PATH not configured | Add the JDK bin directory to PATH |
| JAVA_HOME missing | Create the JAVA_HOME environment variable |
| JAVA_HOME points to bin | Point it to the JDK root folder |
| Wrong JDK version | Update JAVA_HOME and PATH |
| Remove unused versions or prioritize the correct one | Remove unused versions or prioritize the correct one |
Best Practices to Avoid This Error
Follow these recommendations:
- Install the Java Development Kit (JDK) instead of only the JRE.
- Set the JAVA_HOME variable to the JDK installation directory.
- Add $JAVA_HOME/bin or %JAVA_HOME%\bin to the PATH.
- Keep java and javac versions consistent.
- Avoid installing multiple JDK versions unless necessary.
- Verify Java installation immediately after installing a new JDK.
- Restart terminals after modifying environment variables.
- Keep IDE settings synchronized with the system JDK.
- Regularly update to a supported Java version.
Frequently Asked Questions (FAQs)
Why is javac not recognized?
This error usually occurs because:
- The JDK is not installed.
- Only the JRE is installed.
- The PATH environment variable does not include the JDK bin directory.
- JAVA_HOME is missing or incorrectly configured.
What is the difference between java and javac?
- java runs compiled Java applications (.class files).
- javac compiles Java source files (.java) into bytecode (.class).
Both are part of the JDK, but only java is included in the JRE.
How do I verify that javac is installed?
Run:
javac -version
If the compiler is installed and configured correctly, you’ll see output similar to:
javac 21
Should JAVA_HOME point to the bin folder?
No.
Correct:
C:\Program Files\Java\jdk-21
Incorrect:
C:\Program Files\Java\jdk-21\bin
The PATH variable should include the bin folder, while JAVA_HOME should point to the JDK root directory.
Conclusion
The “javac is not recognized as an internal or external command” error is one of the most common Java installation issues. In most cases, it occurs because the JDK is missing, the JAVA_HOME environment variable is not configured, or the JDK bin directory is absent from the system PATH. By installing the correct JDK, setting JAVA_HOME, updating the PATH variable, and verifying the installation with java -version and javac -version, you can quickly resolve this error and create a reliable Java development environment for compiling and running Java applications.