Discover problems
When installing jdk, after installing Java -version, it returns normal, but does not return the value after executing javac
Possible reasons
Command Prompt Window Caching Issues:
Sometimes the Command Prompt window may cache old environment variable settings and require restarting the Command Prompt window.
System path priority issues:
There may be multiple javac commands in the system path, and the system may prefer the wrong javac.
File permission issues:
The current user may not have sufficient permissions to execute javac commands.
Command line input problem:
Sometimes command line input may have hidden special characters or formatting issues.
Search through where javac command, multiple jdk files may exist in the system
C:\Users\Yang>where javac C:\Program Files\Common Files\Oracle\Java\javapath\ D:\Environment\jdk\bin\
There are two javac files in my system
View the java directory
C:\Users\Yang>echo %JAVA_HOME% D:\Environment\jdk
Execute javac using absolute path
C:\Users\Yang>"D:\Environment\jdk\bin\javac" -version javac 1.8.0_181
Very good, use absolute path
"D:\Environment\jdk\bin\javac"
Successfully displayedjavac
version information. This showsjavac
It is normal itself, and the problem may lie in the settings of environment variables or the cache of command prompts.
Steps to solve
now thatjavac
The command can work normally through the absolute path, and we can ensure thatjavac
The command isPATH
It can also work normally in variables:
Step 1: Make sure that D:\Environment\jdk\bin is preferred in the PATH variable
-
Turn on environment variable settings:
- Right-click the "This Computer" or "Computer" icon and select "Properties".
- Click "Advanced System Settings".
- In the System Properties window, click the Environment Variables button.
-
Edit Path variable:
- In the System Variables section, find and select
Path
Variables, and then click the "Edit" button. - make sure
D:\Environment\jdk\bin
The path is before other paths. You can adjust the order by dragging or using the Up button.
- In the System Variables section, find and select
Step 2: Restart the command prompt
-
Close all command prompt windows:
- Close all open command prompt windows.
-
Reopen the command prompt:
- according to
Win + R
Key, entercmd
, and press Enter to open a new command prompt window.
- according to
-
Verify javac command:
- In the new command prompt window, run the following command:
javac -version
- In the new command prompt window, run the following command:
Step 3: Check the PATH variable
-
Verify PATH variable:
- In the command prompt, run the following command to view the current
PATH
Variables:echo %PATH%
- make sure
D:\Environment\jdk\bin
The path appears in the output and takes precedence over other paths.
- In the command prompt, run the following command to view the current
Step 4: Clear the system cache
-
Restart the computer:
- Sometimes the system caches old environment variable settings, and restarting the computer can ensure that all changes take effect.
Step 5: Check the JAVA_HOME variable
-
Verification JAVA_HOME variable:
- In the command prompt, run the following command to view
JAVA_HOME
Variables:echo %JAVA_HOME%
- make sure
JAVA_HOME
Variables point to the correct JDK installation directory, for example:D:\Environment\jdk
- In the command prompt, run the following command to view
Example steps
-
Edit Path variable:
- Open Control Panel -> System and Security -> System -> Advanced System Settings.
- Click the "Environment Variables" button.
- In the System Variables section, find and select
Path
Variables, and then click the "Edit" button. - make sure
D:\Environment\jdk\bin
The path is before another path, and then click "OK" to save the changes.
-
Restart the command prompt:
- Close all command prompt windows.
- according to
Win + R
Key, entercmd
, and press Enter to open a new command prompt window.
-
Verify javac command:
- In the new command prompt window, run the following command:
javac -version
- In the new command prompt window, run the following command:
Through the above steps, you should be able to ensure thatjavac
The command isPATH
It can also work normally in variables.
Summarize
This is the article about the inability to run javac commands in cmd (java commands can run). For more related contents in cmd, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!