Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

java - javac is not recognized as an internal or external command, operable program or batch file


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

TL;DR

For experienced readers:

  1. Find the Java path; it looks like this: C:Program FilesJavajdkxxxxin
  2. Start-menu search for "environment variable" to open the options dialog.
  3. Examine PATH. Remove old Java paths.
  4. Add the new Java path to PATH.
  5. Edit JAVA_HOME.
  6. Close and re-open console/IDE.

Welcome!

You have encountered one of the most notorious technical issues facing Java beginners: the 'xyz' is not recognized as an internal or external command... error message.

In a nutshell, you have not installed Java correctly. Finalizing the installation of Java on Windows requires some manual steps. You must always perform these steps after installing Java, including after upgrading the JDK.

Environment variables and PATH

(If you already understand this, feel free to skip the next three sections.)

When you run javac HelloWorld.java, cmd must determine where javac.exe is located. This is accomplished with PATH, an environment variable.

An environment variable is a special key-value pair (e.g. windir=C:WINDOWS). Most came with the operating system, and some are required for proper system functioning. A list of them is passed to every program (including cmd) when it starts. On Windows, there are two types: user environment variables and system environment variables.

You can see your environment variables like this:

C:>set
ALLUSERSPROFILE=C:ProgramData
APPDATA=C:UserscraigAppDataRoaming
CommonProgramFiles=C:Program FilesCommon Files
CommonProgramFiles(x86)=C:Program Files (x86)Common Files
CommonProgramW6432=C:Program FilesCommon Files
...

The most important variable is PATH. It is a list of paths, separated by ;. When a command is entered into cmd, each directory in the list will be scanned for a matching executable.

On my computer, PATH is:

C:>echo %PATH%
C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPower
Shellv1.0;C:ProgramDataMicrosoftWindowsStart MenuPrograms;C:UserscraigAppData
RoamingMicrosoftWindowsStart MenuPrograms;C:msys64usrin;C:msys64mingw64in;C:
msys64mingw32in;C:Program Files
odejs;C:Program Files (x86)Yarnin;C:Users
craigAppDataLocalYarnin;C:Program FilesJavajdk-10.0.2in;C:ProgramFilesGitcmd;
C:Program FilesOracleVirtualBox;C:Program Files7-Zip;C:Program FilesPuTTY;C:
Program Fileslaunch4j;C:Program Files (x86)NSISBin;C:Program Files (x86)Common Files
AdobeAGL;C:Program FilesIntelIntel(R) Management Engine ComponentsDAL;C:Program
FilesIntelIntel(R) Management Engine ComponentsIPT;C:Program FilesInteliCLS Client;
C:Program Files (x86)IntelIntel(R) Management Engine ComponentsDAL;C:Program Files
(x86)IntelIntel(R) Management Engine ComponentsIPT;C:Program Files (x86)InteliCLS
Client;C:UserscraigAppDataLocalMicrosoftWindowsApps

When you run javac HelloWorld.java, cmd, upon realizing that javac is not an internal command, searches the system PATH followed by the user PATH. It mechanically enters every directory in the list, and checks if javac.com, javac.exe, javac.bat, etc. is present. When it finds javac, it runs it. When it does not, it prints 'javac' is not recognized as an internal or external command, operable program or batch file.

You must add the Java executables directory to PATH.

JDK vs. JRE

(If you already understand this, feel free to skip this section.)

When downloading Java, you are offered a choice between:

  • The Java Runtime Environment (JRE), which includes the necessary tools to run Java programs, but not to compile new ones – it contains java but not javac.
  • The Java Development Kit (JDK), which contains both java and javac, along with a host of other development tools. The JDK is a superset of the JRE.

You must make sure you have installed the JDK. If you have only installed the JRE, you cannot execute javac because you do not have an installation of the Java compiler on your hard drive. Check your Windows programs list, and make sure the Java package's name includes the words "Development Kit" in it.

Don't use set

(If you weren't planning to anyway, feel free to skip this section.)

Several other answers recommend executing some variation of:

C:>:: DON'T DO THIS
C:>set PATH=C:Program FilesJavajdk1.7.0_09in

Do not do that. There are several major problems with that command:

  1. This command erases everything else from PATH and replaces it with the Java path. After executing this command, you might find various other commands not working.
  2. Your Java path is probably not C:Program FilesJavajdk1.7.0_09in – you almost definitely have a newer version of the JDK, which would have a different path.
  3. The new PATH only applies to the current cmd session. You will have to reenter the set command every time you open Command Prompt.

Points #1 and #2 can be solved with this slightly better version:

C:>:: DON'T DO THIS EITHER
C:>set PATH=C:Program FilesJava<enter the correct Java folder here>in;%PATH%

But it is just a bad idea in general.

Find the Java path

The right way begins with finding where you have installed Java. This depends on how you have installed Java.

Exe installer

You have installed Java by running a setup program. Oracle's installer places versions of Java under C:Program FilesJava (or C:Program Files (x86)Java). With File Explorer or Command Prompt, navigate to that directory.

Each subfolder represents a version of Java. If there is only one, you have found it. Otherwise, choose the one that looks like the newer version. Make sure the folder name begins with jdk (as opposed to jre). Enter the directory.

Then enter the bin directory of that.

You are now in the correct directory. Copy the path. If in File Explorer, click the address bar. If in Command Prompt, copy the prompt.

The resulting Java path should be in the form of (without quotes):

C:Program FilesJavajdkxxxxin

Zip file

You have downloaded a .zip containing the JDK. Extract it to some random place where it won't get in your way; C:Java is an acceptable choice.

Then locate the bin folder somewhere within it.

You are now in the correct directory. Copy its path. This is the Java path.

Remember to never move the folder, as that would invalidate the path.

Open the settings dialog

That is the dialog to edit PATH. There are numerous ways to get to that dialog, depending on your Windows version, UI settings, and how messed up your system configuration is.

Try some of these:

  • Start Menu/taskbar search box ? search for "environment variable"
  • Win + R ? control sysdm.cpl,,3
  • Win + R ? SystemPropertiesAdvanced.exe ? Environment Variables
  • File Explorer ? type into address bar Control PanelSystem and SecuritySystem ? Advanced System Settings (far left, in sidebar) ? Environment Variables
  • Desktop ? right-click This PC ? Properties ? Advanced System Settings ? Environment Variables
  • Start Menu ? right-click Computer ? Properties ? Advanced System Settings ? Environment Variables
  • Control Panel (icon mode) ? System ? Advanced System Settings ? Environment Variables
  • Control Panel (category mode) ? System and Security ? System ? Advanced System Settings ? Environment Variables
  • Desktop ? right-click My Computer ? Advanced ? Environment Variables
  • Control Panel ? System ? Advanced ? Environment Variables

Any of these should take you to the right settings dialog.

If you are on Windows 10, Microsoft has blessed you with a fancy new UI to edit PATH. Otherwise, you will see PATH in its full semicolon-encrusted glory, squeezed into a single-line textbox. Do your best to make the necessary edits without breaking your system.

Clean PATH

Look at PATH. You almost definitely have two PATH variables (because of user vs. system environment variables). You need to look at both of them.

Check for other Java paths and remove them. Their existence can cause all sorts of conflicts. (For instance, if you have JRE 8 and JDK 11 in PATH, in that order, then javac will invoke the Java 11 compiler, which will create version 55 .class files, but java will invoke the Java 8 JVM, which only supports up to version 52, and you will experience unsupported version errors and not be able to compile and run any programs.) Sidestep these problems by making sure you only have one Java path in PATH. And while you're at it, you may as well uninstall old Java versions, too. And remember that you don't need to have both a JDK and a JRE.

If you have C:ProgramDataOracleJavajavapath, remove that as well. Oracle intended to solve the problem of Java paths breaking after upgrades by creating a symbolic link t


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...