To install Java on Windows 11 using the command line, the most efficient method is using the Windows Package Manager (winget), which allows you to download and install the Java Development Kit (JDK) with a single command without manually navigating websites. This process achieves a full installation of the Java environment, enabling you to compile and run Java applications directly from the Command Prompt or PowerShell.
What You'll Need
- A computer running Windows 11.
- An active internet connection to download the JDK packages.
- Administrative privileges on the user account to allow system-level installations and environment variable changes.
- Windows Terminal, Command Prompt, or PowerShell (pre-installed on Windows 11).
Step-by-Step
- Open the Terminal as Administrator
Right-click the Start button and select Terminal (Admin) or Windows PowerShell (Admin). It is critical to run the terminal as an administrator because installing software into the Program Files directory and modifying system environment variables requires elevated permissions. If you run it as a standard user, the installation may fail or you will be unable to configure the PATH variable later.
- Search for Available Java Versions
Before installing, you should identify which version of the JDK is available and the exact ID needed for the command. Type the following command and press Enter:
winget search OpenJDK
This will display a list of available Java distributions, such as Microsoft Build of OpenJDK, Oracle JDK, or Eclipse Temurin. Note the "Id" column for the version you prefer (e.g.,Microsoft.OpenJDK.17). - Execute the Installation Command
Once you have identified the correct ID, use the install command. For example, to install the Microsoft OpenJDK 17, type:
winget install Microsoft.OpenJDK.17
Press Enter. The system will begin downloading the installer from the official source and execute the installation process in the background. You will see a progress bar in the terminal window. - Verify the Installation
After the terminal indicates that the installation was successful, you must verify that the Java executable is recognized. Close your current terminal window and open a new one to ensure all environment changes are refreshed. Type:
java -version
If the installation worked, the terminal will return the version number of the JDK you installed. If it returns an error saying the command is not recognized, you will need to manually configure the environment variables. - Configure the JAVA_HOME Environment Variable
Many Java-based applications and IDEs require a specific system variable called JAVA_HOME to locate the JDK. Since winget handles the software installation but not always the custom variable creation, do this manually:
1. Open the Start menu, search for "Edit the system environment variables", and select it. (Note: Menu labels may vary slightly depending on your specific Windows 11 build or region).
2. In the System Properties window, click the "Environment Variables" button at the bottom.
3. Under "System variables", click "New".
4. For the Variable name, enterJAVA_HOME.
5. For the Variable value, enter the path where Java was installed. This is typicallyC:\Program Files\Microsoft\jdk-17.x.x...(navigate to the folder in File Explorer to copy the exact path).
6. Click OK on all windows to save the changes. - Update the System Path
To ensure the
javaandjavaccommands work from any folder in the command line, the bin folder must be in your Path variable:
1. In the same "Environment Variables" window, find the variable named "Path" under System variables and select "Edit".
2. Click "New" and add%JAVA_HOME%\bin.
3. Click OK to close the window. This tells Windows to look inside the Java bin directory whenever you type a Java command.
Common Mistakes
One of the most frequent errors is failing to restart the terminal after installation. Environment variables are loaded when a shell session starts; if you keep the same window open, the java -version command will continue to fail because the session doesn't know the path has changed.
Another common mistake is installing the Java Runtime Environment (JRE) instead of the Java Development Kit (JDK). The JRE only allows you to run Java apps, while the JDK is required for development, compiling code, and using tools like javac. If you only see the java command working but javac fails, you likely installed the JRE.
Users also frequently forget to run the terminal as an administrator. When using winget, if the prompt for elevation appears and is declined, the installation will either abort or install the software in a user-specific directory that is not automatically added to the system path, leading to "command not found" errors.
Tips
If you need to manage multiple versions of Java for different projects, consider using a version manager rather than winget. While winget is great for a single system-wide installation, version managers allow you to switch between Java 8, 11, 17, and 21 without manually editing environment variables every time.
To keep your Java installation updated, you can periodically run winget upgrade --all in the terminal. This will check for newer versions of the JDK and update them automatically, ensuring you have the latest security patches and performance improvements.
Always double-check the path in the JAVA_HOME variable. A common error is adding a trailing backslash (e.g., C:\Java\) which can cause some older Java applications to fail because they append their own slash, resulting in a double-slash path that the system cannot resolve.
Troubleshooting
If the java -version command still returns an error after following all steps, first check if the installation actually occurred. Navigate to C:\Program Files and look for a folder named "Microsoft", "Oracle", or "Eclipse Adoptium". If the folder is missing, the winget installation failed; try running the command again or check your internet connection.
If the folder exists but the command fails, the issue is almost certainly the Path variable. Go back to the Environment Variables settings and ensure there are no typos in %JAVA_HOME%\bin. Ensure that the JAVA_HOME variable itself points exactly to the root folder of the JDK, not the bin folder. For example, JAVA_HOME should be C:\Program Files\Java\jdk-17, and the Path should be %JAVA_HOME%\bin.
If you encounter a "Permission Denied" error during the winget process, verify that your antivirus software is not blocking the installation. Some aggressive security suites flag the automated installation of binaries via command line as suspicious activity. Temporarily disable the real-time shield or add the terminal to the allowed applications list.
In cases where the system recognizes an old version of Java instead of the new one, check the Path variable for any other entries containing "Java" or "Oracle". Windows searches the Path list from top to bottom. Move your new %JAVA_HOME%\bin entry to the very top of the list using the "Move Up" button in the Path editor to give it priority over older versions.
Frequently Asked Questions
Do I need to pay for the JDK?
Many distributions, such as the Microsoft Build of OpenJDK and Eclipse Temurin, are free and open-source. However, some Oracle JDK versions may require a license for commercial use. Always check the license of the specific ID you choose in winget.
What is the difference between JDK and JRE?
The JRE (Java Runtime Environment) is a subset of the JDK (Java Development Kit). The JRE is only for running Java programs, whereas the JDK includes the JRE plus development tools like the compiler (javac) and debugger.
Can I install multiple versions of Java at once?
Yes, you can have multiple versions installed in different folders. To switch between them, you must update the JAVA_HOME environment variable to point to the folder of the version you wish to use and restart your terminal.
Why does the terminal say "java is not recognized" after installation?
This happens because the folder containing the java.exe file has not been added to the system's PATH environment variable, or the terminal was not restarted after the path was added.
Does winget work on all versions of Windows 11?
Yes, winget (Windows Package Manager) is built into Windows 11. If for some reason it is missing, you can install the "App Installer" from the official Microsoft Store to enable it.
Which Java version should I choose?
For most modern development and compatibility, Java 17 or Java 21 are recommended as they are Long-Term Support (LTS) versions, meaning they receive updates and support for a longer period than non-LTS versions.