When developing in Java, there are two classifications of errors that you can receive: run-time and compilation errors. A run-time error occurs when the program you’re building compiles and converts successfully into bytecodes but fails to run properly, while a compilation error is when the program fails to compile entirely. In this article, we’ll be covering the compilation error message, “javac is not recognized,” which pops up when trying to compile a .java file using the javac command in either your command prompt (CMD) or PowerShell utilities.
What is the javac command?
When working with the Java Development Kit (JDK), the primary compiling command used is javac. If you’re new to Java development, the Java compiler is a tool or program used to translate or convert Java source files into Java class files (.class). These .class files contain bytecodes, which are highly optimized sets of instructions (binary code) that the Java Virtual Machine (JVM) needs in order to execute the program. During the conversion process, the Javac command will read through your Java source files, which should contain your module, package, and type declarations (written in Java), and generate the .class files that contain your bytecodes.
What is a Java Virtual Machine (JVM)?
A Java Virtual Machine (JVM) is a virtual machine or engine that provides Java with a runtime environment so that Java-based applications can run on your computer. The JVM will take the bytecodes that you’ve compiled with the javac command and convert them into the machine code that your particular system can understand. It’s important to note here that the JVM can run on multiple platforms, but the bytecodes that it outputs are platform-dependent. So, if you were to take your .class files and try to run them on a different system, you would have to recompile them for that particular system.
What does the “javac is not recognized” error message mean?
Now that we’ve gone over what javac is, what it is used for, and touched on what a Java Virtual Machine is, let’s go over what this error means precisely. If using the command prompt (CMD) or the PowerShell utilities in Windows, you’re going to see a full error text that says: “Javac is not recognized as an internal or external command, operable program, or batch file,” which means that your compiler cannot find the javac.exe file. In other words, your computer is struggling to run the javac command because it isn’t set up properly, and thus the Java command is not found.
Why you may get the “javac is not recognized” error message
There are a few reasons why you may get the “javac is not recognized as an internal or external command, operable program, or batch file” error. There are generally two reasons why you would see this error message:
- You’re missing the Java Development Kit (JDK). A common mistake among new Java developers is to download and install the Java Runtime Environment and assume that you would also receive the Javac compiler tool with it. While you do need the Java Runtime Environment to run already built Java applications, you will need to download and install the Java Development Kit as well, with which the Javac compiler comes bundled.
- You have set the incorrect path to Javac. If you do have Java installed, then your PATH environment may point to the wrong Java executable directory. For your system to be able to recognize and run the javac command, you have to ensure that both the JAVA_HOME and PATH environment variables are properly configured on your computer. We will cover a little bit more below about Java PATH variables.
Both of these scenarios are quite common but also offer up the question of “what exactly are JDK and the PATH environment variable?” Let’s take a moment to understand these further.
What you need to know about the Java Development Kit and the PATH environment variable
The Java Development Kit (JDK) is what you will need to install in order to be able to compile your .java files into bytecodes using the javac command. The JDK not only contains the Javac compiler tool but also has other tools that are used for debugging, creating documentation, and running applications that are written in the Java programming language. You can download the latest JDK here.
In order for your system to be able to recognize the Javac command, you will need to properly set your PATH environment variable. The PATH environment variable is a system-wide variable that tells your operating system where it can find specific executables or binaries. When you install the JDK, it will add the location of javac to your PATH variable. However, if you installed the JDK in a location that is not already in your PATH variable, then you will need to add it manually.
How to check your PATH environment variable
You can check if javac is already in your PATH variable by opening up the command prompt (CMD) or PowerShell and typing in “echo %PATH%”.
This will print out the current PATH variable for your system. If you do not see the location of your JDK’s bin folder, then you will need to add it. You can find out how to add in the section below to correct the “Javac is not recognized” error.
How to fix the javac is not recognized error message
As stated above, there are two main scenarios where this error may occur, each of which is relatively simple to correct. Follow the steps within the scenarios below to resolve your javac command not being found.
Scenario 1: Downloading and Installing the Java Development Kit
If you do not have the JDK installed on your system, then all you need to do is download and install it. As previously stated, the JDK comes bundled with the javac compiler tool, so this will resolve your issue.
Do the following:
- Start by opening your web browser and either searching for “Java Downloads JDK” and clicking the link for oracle.com, or visit the website directly via this link.
- Scroll down to the latest Java SE Development Kit downloads, select the option for Windows, then click on each file link to begin downloading them.
- Accept the license agreement, then, when prompted, select the option to download the .exe file.
- Once you’ve finished downloading the file, locate it in your downloads, open it and follow the instructions in the setup wizard to complete the installation. Ensure that the Source Code, Development Tools, and all the subfeatures of each are set to install on your local hard drive. Be sure to take note of the entire installation path.
- Once the installation has finished, restart your computer (you should be prompted to do this).
Keep in mind that the installed JDK may still need to be configured as these are simply the steps for acquiring it. The next set of steps will show you exactly how to reconfigure the PATH environment variable if necessary.
Scenario 2: Setting the PATH variable correctly
When installing the JDK, it should automatically add javac to your PATH variable. However, if it does not, and you’re still getting the “javac is not recognized as an internal or external command, operable program, or batch file” error, then you’ll need to perform a series of manual steps to finalize the installation of the JDK kit.
To do this:
- The first step is to open up your System Properties Window. On Windows, hit the Windows key + R to open the Run Dialogue box.
- In the Run Dialogue box, type in “sysdm.cpl” and hit the OK button.
- In the new Systems Properties Window, click on the Advanced tab at the top of the window.
- In the Advanced tab, look for an Environment Variables button and click it.
- In the System Variables window, click on the New button.
- An Edit System Variable Window will pop open. Here, you will want to add a Variable Name and Variable Value. The name should be JAVA_HOME, and the value should be the path to your Java Development Kit Directory. Once these are set, hit the OK button.
- When you hit OK, you should be returned to the Environment Variables window. Here, you’ll want to look for PATH under the System Variables section. Once you find it, select it, and click on the Edit button.
- Now you’ll be in the Edit Environment window. Simply click on the New button.
- In the new environment variable that comes up, name it the following: %JAVA_HOME%\bin and hit Enter. Then hit the OK button to apply the changes.
Once you’ve successfully completed the above steps, your Java PATH Environment should be properly configured, and you should be able to compile your Java applications with the Command Prompt (CMD) or the PowerShell utility.
If you want to check if the configuration was successful, open up your Command Prompt, type in “echo %JAVA_HOME%”, and hit Enter. If the Command Prompt returns the Java PATH with the JDK version you’ve installed, you’ve succeeded. If there is just “space” instead of a JDK version, then it’s been unsuccessful, and you’ll need to start again from the top.