javac 不是内部或外部命令,也不是可运行的程序或批处理文件 [英] javac is not recognized as an internal or external command, operable program or batch file

查看:32
本文介绍了javac 不是内部或外部命令,也不是可运行的程序或批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试编译 Java 程序时遇到错误.

我使用的是 Windows(这是 Windows 特定的问题)并且我安装了最新的 JDK.

我尝试了一个涉及 PATH 变量的解决方案,但错误仍然存​​在.

控制台输出:

C:\>set path=C:Program Files (x86)\Java\jdk1.7.0\binC:\>javac Hello.java'javac' 未被识别为内部或外部命令,可运行的程序或批处理文件.

解决方案

TL;DR

对于有经验的读者:

  1. 找到Java路径;它看起来像这样:C:\Program Files\Java\jdkxxxx\bin\
  2. 开始菜单搜索环境变量"以打开选项对话框.
  3. 检查PATH.删除旧的 Java 路径.
  4. 将新的 Java 路径添加到 PATH.
  5. 编辑JAVA_HOME.
  6. 关闭并重新打开控制台/IDE.

<小时>

欢迎!

您遇到了 Java 初学者面临的最臭名昭著的技术问题之一:'xyz' 未被识别为内部或外部命令... 错误消息.

简而言之,您没有正确安装 Java.在 Windows 上完成 Java 的安装需要一些手动步骤.在安装 Java 之后,包括升级 JDK 之后,您必须始终执行这些步骤.

环境变量和PATH

(如果你已经理解了这一点,可以跳过接下来的三个部分.)

当您运行 javac HelloWorld.java 时,cmd 必须确定 javac.exe 所在的位置.这是通过环境变量 PATH 实现的.

来编辑 PATH.否则,您将看到 PATH 充满分号的光彩,挤在一个单行文本框中.尽最大努力在不破坏系统的情况下进行必要的编辑.

清理PATH

查看PATH.您几乎肯定有两个 PATH 变量(因为用户与系统环境变量).你需要看看他们两个.

检查其他 Java 路径并删除它们.它们的存在会引起各种冲突.(例如,如果您在 PATH 中有 JRE 8 和 JDK 11,那么 javac 将调用 Java 11 编译器,这将创建版本 55 .class 文件,但是 java 会调用 Java 8 JVM,它只支持版本 52,你会体验到 不支持的版本错误,并且无法编译和运行任何程序.)通过确保您避免这些问题PATH 中只有一个 Java 路径.在此期间,您也可以卸载旧的 Java 版本,也.请记住,您不需要同时拥有 JDK 和一个 JRE.

如果您有 C:\ProgramData\Oracle\Java\javapath也将其删除.Oracle 打算通过创建一个始终指向的 符号链接 来解决升级后 Java 路径中断的问题到最新的 Java 安装.不幸的是,它通常最终指向错误的位置或 根本不起作用.最好去掉这个条目,手动管理Java路径.

现在也是在 PATH 上执行一般内务处理的好机会.如果您有与 PC 上不再安装的软件相关的路径,您可以删除它们.您还可以打乱路径的顺序(如果您关心类似的事情).

添加到PATH

现在把你三步前找到的Java路径放到系统PATH中.

新路径在列表中的位置无关紧要;把它放在最后是一个不错的选择.

如果您使用的是 Windows 10 之前的 UI,请确保分号放置正确.应该有一个分隔列表中的每条路径.

这里真的没什么好说的.只需将路径添加到 PATH 并单击确定".

设置JAVA_HOME

在此期间,您也可以设置 JAVA_HOME.这是另一个还应包含 Java 路径的环境变量.许多 Java 和非 Java 程序,包括流行的 Java 构建系统 MavenGradle,如果设置不正确会抛出错误.

如果 JAVA_HOME 不存在,则将其创建为新的系统环境变量.设置为不含bin/目录的Java目录路径,即C:\Program Files\Java\jdkxxxx\.

升级Java后记得编辑JAVA_HOME.

关闭并重新打开命令提示符

虽然修改了PATH,但是所有运行的程序,包括cmd,只能看到旧的PATH.这是因为所有环境变量的列表仅在程序开始执行时才被复制到程序中;此后,它只查询缓存的副本.

没有好的方法刷新 cmd 的环境变量,因此只需关闭命令提示符并再次打开它.如果您使用的是 IDE,请关闭并重新打开它.

另见

I am experiencing an error while trying to compile Java programs.

I am on Windows (this is a Windows-specific problem) and I have the latest JDK installed.

I have attempted a solution involving the PATH variable, but the error persists.

Console output:

C:\>set path=C:Program Files (x86)\Java\jdk1.7.0\bin
C:\>javac Hello.java
'javac' is not recognized as an internal or external command,
operable program or batch file.

解决方案

TL;DR

For experienced readers:

  1. Find the Java path; it looks like this: C:\Program Files\Java\jdkxxxx\bin\
  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:\Users\craig\AppData\Roaming
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common 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:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPower
Shell\v1.0\;C:\ProgramData\Microsoft\Windows\Start Menu\Programs;C:\Users\craig\AppData\
Roaming\Microsoft\Windows\Start Menu\Programs;C:\msys64\usr\bin;C:\msys64\mingw64\bin;C:\
msys64\mingw32\bin;C:\Program Files\nodejs\;C:\Program Files (x86)\Yarn\bin\;C:\Users\
craig\AppData\Local\Yarn\bin;C:\Program Files\Java\jdk-10.0.2\bin;C:\ProgramFiles\Git\cmd;
C:\Program Files\Oracle\VirtualBox;C:\Program Files\7-Zip\;C:\Program Files\PuTTY\;C:\
Program Files\launch4j;C:\Program Files (x86)\NSIS\Bin;C:\Program Files (x86)\Common Files
\Adobe\AGL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program
Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\iCLS Client\;
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files
(x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\iCLS
Client\;C:\Users\craig\AppData\Local\Microsoft\WindowsApps

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 Files\Java\jdk1.7.0_09\bin

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 Files\Java\jdk1.7.0_09\bin – 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 Files\Java\<enter the correct Java folder here>\bin;%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 Files\Java\ (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 Files\Java\jdkxxxx\bin\

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 Panel\System and Security\System » 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:\ProgramData\Oracle\Java\javapath, remove that as well. Oracle intended to solve the problem of Java paths breaking after upgrades by creating a symbolic link that would always point to the latest Java installation. Unfortunately, it often ends up pointing to the wrong location or simply not working. It is better to remove this entry and manually manage the Java path.

Now is also a good opportunity to perform general housekeeping on PATH. If you have paths relating to software no longer installed on your PC, you can remove them. You can also shuffle the order of paths around (if you care about things like that).

Add to PATH

Now take the Java path you found three steps ago, and place it in the system PATH.

It shouldn't matter where in the list your new path goes; placing it at the end is a fine choice.

If you are using the pre-Windows 10 UI, make sure you have placed the semicolons correctly. There should be exactly one separating every path in the list.

There really isn't much else to say here. Simply add the path to PATH and click OK.

Set JAVA_HOME

While you're at it, you may as well set JAVA_HOME as well. This is another environment variable that should also contain the Java path. Many Java and non-Java programs, including the popular Java build systems Maven and Gradle, will throw errors if it is not correctly set.

If JAVA_HOME does not exist, create it as a new system environment variable. Set it to the path of the Java directory without the bin/ directory, i.e. C:\Program Files\Java\jdkxxxx\.

Remember to edit JAVA_HOME after upgrading Java, too.

Close and re-open Command Prompt

Though you have modified PATH, all running programs, including cmd, only see the old PATH. This is because the list of all environment variables is only copied into a program when it begins executing; thereafter, it only consults the cached copy.

There is no good way to refresh cmd's environment variables, so simply close Command Prompt and open it again. If you are using an IDE, close and re-open it too.

See also

这篇关于javac 不是内部或外部命令,也不是可运行的程序或批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆