Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - javac不被识别为内部或外部命令,可操作程序或批处理文件(javac is not recognized as an internal or external command, operable program or batch file [closed])

I am experiencing an error while trying to compile Java programs. (尝试编译Java程序时遇到错误。)

I am on Windows (this is a Windows-specific problem) and I have the latest JDK installed. (我在Windows上(这是Windows特定的问题),并且我安装了最新的JDK。)

I have attempted a solution involving the PATH variable, but the error persists. (我尝试了涉及PATH变量的解决方案,但错误仍然存??在。)

Console output: (控制台输出:)

C:>set path=C:Program Files (x86)Javajdk1.7.0in
C:>javac Hello.java
'javac' is not recognized as an internal or external command,
operable program or batch file.
  ask by user987137 translate from so

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

1 Reply

0 votes
by (71.8m points)

TL;DR (TL; DR)

For experienced readers: (对于有经验的读者:)

  1. Find the Java path; (查找Java路径;) it looks like this: C:\Program Files\Java\jdkxxxx\bin\ (它看起来像这样: C:\Program Files\Java\jdkxxxx\bin\)
  2. Start-menu search for "environment variable" to open the options dialog. (在开始菜单中搜索“环境变量”以打开选项对话框。)
  3. Examine PATH . (检查PATH 。) Remove old Java paths. (删除旧的Java路径。)
  4. Add the new Java path to PATH . (将新的Java路径添加到PATH 。)
  5. Edit JAVA_HOME . (编辑JAVA_HOME 。)
  6. Close and re-open console/IDE. (关闭并重新打开控制台/ 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. (您遇到了Java初学者面临的最臭名昭著的技术问题之一: 'xyz' is not recognized as an internal or external command...错误消息。)

In a nutshell, you have not installed Java correctly . (简而言之, 您尚未正确安装Java 。) Finalizing the installation of Java on Windows requires some manual steps. (完成Windows上Java的安装需要一些手动步骤。) You must always perform these steps after installing Java, including after upgrading the JDK. (安装Java之后,包括升级JDK之后,必须始终执行这些步骤。)

Environment variables and PATH (环境变量和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. (运行javac HelloWorld.java ,cmd必须确定javac.exe的位置。) This is accomplished with PATH , an environment variable. (这是通过环境变量PATH完成的。)

An environment variable is a special key-value pair (eg windir=C:\WINDOWS ). (环境变量是一个特殊的键值对(例如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. (它们的列表在启动时传递给每个程序(包括cmd)。) On Windows , there are two types : user environment variables and system environment variables . (在Windows上 ,有两种类型用户环境变量系统环境变量 。)

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 . (最重要的变量是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. (在cmd中输入命令时,将扫描列表中的每个目录以查找匹配的可执行文件。)

On my computer, PATH is: (在我的计算机上, PATH是:)

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 . (当您运行javac HelloWorld.java cmd时,一旦意识到javac不是内部命令 ,请先搜索系统PATH然后搜索用户PATH 。) It mechanically enters every directory in the list, and checks if javac.com , javac.exe , javac.bat , etc. is present. (它自动进入列表中的每个目录,并检查是否存在javac.comjavac.exejavac.bat 。) When it finds javac , it runs it. (找到javac ,它将运行它。) When it does not, it prints 'javac' is not recognized as an internal or external command, operable program or batch file. (如果不是,它将打印'javac' is not recognized as an internal or external command, operable program or batch file.)

You must add the Java executables directory to PATH . (您必须将Java可执行文件目录添加到PATH 。)

JDK vs. JRE (JDK与JRE)

(If you already understand this, feel free to skip this section.) ((如果您已经了解这一点,请随时跳过本节。))

When downloading Java, you are offered a choice between: (下载Java时,可以选择以下选项:)

  • 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 . (Java Runtime Environment (JRE)包含运行Java程序但编译新程序的必要工具-它包含java而不包含javac 。)
  • The Java Development Kit (JDK), which contains both java and javac , along with a host of other development tools. (Java开发工具包 (JDK),包含javajavac ,以及许多其他开发工具。) The JDK is a superset of the JRE. (JDK是JRE的超集。)

You must make sure you have installed the JDK . (您必须确保已安装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. (如果仅安装了JRE,则无法执行javac因为在硬盘驱动器上没有安装Java编译器。) Check your Windows programs list, and make sure the Java package's name includes the words "Development Kit" in it. (检查Windows程序列表,并确保Java软件包的名称中包含“ Development Kit”字样。)

Don't use set (不要使用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. (此命令将从PATH 删除所有其他内容 ,并将其替换为Java路径。) After executing this command, you might find various other commands not working. (执行此命令后,您可能会发现其他各种命令不起作用。)
  2. Your Java path is probably not C:\Program Files\Java\jdk1.7.0_09\bin – you almost definitely have a newer version of the JDK, which would have a different path. (您的Java路径可能不是 C:\Program Files\Java\jdk1.7.0_09\bin您几乎可以肯定拥有一个较新版本的JDK,但路径却有所不同。)
  3. The new PATH only applies to the current cmd session. (新的PATH</cod

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...