SoFunction
Updated on 2025-04-07

The javac command in cmd cannot be run (java commands can run) Solution steps

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 displayedjavacversion information. This showsjavacIt is normal itself, and the problem may lie in the settings of environment variables or the cache of command prompts.

Steps to solve

now thatjavacThe command can work normally through the absolute path, and we can ensure thatjavacThe command isPATHIt 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 selectPathVariables, and then click the "Edit" button.
    • make sureD:\Environment\jdk\binThe path is before other paths. You can adjust the order by dragging or using the Up button.

Step 2: Restart the command prompt

  • Close all command prompt windows

    • Close all open command prompt windows.
  • Reopen the command prompt

    • according toWin + RKey, 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
      

Step 3: Check the PATH variable

  • Verify PATH variable
    • In the command prompt, run the following command to view the currentPATHVariables:
      echo %PATH%
      
    • make sureD:\Environment\jdk\binThe path appears in the output and takes precedence over other paths.

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 viewJAVA_HOMEVariables:
      echo %JAVA_HOME%
      
    • make sureJAVA_HOMEVariables point to the correct JDK installation directory, for example:
      D:\Environment\jdk
      

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 selectPathVariables, and then click the "Edit" button.
    • make sureD:\Environment\jdk\binThe path is before another path, and then click "OK" to save the changes.
  • Restart the command prompt

    • Close all command prompt windows.
    • according toWin + RKey, 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
      

Through the above steps, you should be able to ensure thatjavacThe command isPATHIt 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!