javac 不被识别为内部或外部命令、可运行程序或批处理文件

发布于 2024-12-08 20:43:16 字数 1470 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

海风掠过北极光 2024-12-15 20:43:16

TL;DR

对于有经验的读者:

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

欢迎!

您遇到了 Java 初学者面临的最臭名昭著的技术问题之一:'xyz' is not recognize as an inside or external command... 错误消息。

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

环境变量和 PATH

(如果您已经了解这一点,请随意跳过接下来的三部分。)

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

环境变量是一个特殊的键值对(例如windir=C:\WINDOWS)。大多数都随操作系统一起提供,有些是系统正常运行所必需的。它们的列表会在每个程序(包括 cmd)启动时传递给它。 在 Windows 上,有两种类型用户环境变量系统环境变量。

您可以这样查看环境变量:

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
...

最重要的变量是 PATH 。它是一个路径列表,由 ; 分隔。当在 cmd 中输入命令时,将扫描列表中的每个目录以查找匹配的可执行文件。

在我的计算机上,PATH 是:

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

当您运行 javac HelloWorld.java、cmd 时,意识到 javac 不是 内部命令,搜索系统 PATH 后跟用户PATH。它会自动输入列表中的每个目录,并检查 存在 javac.comjavac.exejavac.bat。当它找到javac时,它就会运行它。如果不存在,则会打印'javac' is not recognize as an inside or external command, operable program or批处理文件。

您必须将Java可执行文件目录添加到PATH

JDK 与 JRE

(如果您已经了解这一点,请随意跳过本节。)

下载 Java 时,您可以在以下选项之间进行选择:

  • Java 运行时环境 (JRE),其中包括运行 Java 程序所需的工具,但用于编译新程序 –它包含 java 但不包含 javac
  • Java 开发工具包 (JDK),其中包含 java 和 < code>javac,以及许多其他开发工具。 JDK 是 JRE 的超集。

您必须确保您已经安装了 JDK。如果您只安装了 JRE,则无法执行 javac,因为您的硬盘上没有安装 Java 编译器。检查您的 Windows 程序列表,并确保 Java 包的名称中包含“Development Kit”一词。

不要使用 set

(如果您不打算这样做,请随意跳过本节。)

其他几个答案建议执行以下操作的一些变体:

C:\>:: DON'T DO THIS
C:\>set PATH=C:\Program Files\Java\jdk1.7.0_09\bin

不要这样做。该命令存在几个主要问题:

  1. 该命令从 PATH删除所有其他内容,并将其替换为 Java 路径。执行此命令后,您可能会发现其他各种命令不起作用。
  2. 您的 Java 路径可能不是 C:\Program Files\Java\jdk1.7.0_09\bin – 您几乎肯定有一个更新版本的 JDK,它会有一个不同的路径。
  3. 新的 PATH 仅适用当前 cmd 会话。每次打开命令提示符时,您都必须重新输入 set 命令。

第 1 点和第 2 点可以用这个稍微好一点的版本来解决:

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

但总的来说,这只是一个坏主意。

查找 Java 路径

正确的方法是从查找安装 Java 的位置开始。这取决于您安装 Java 的方式

Exe 安装程序

您已通过运行安装程序安装了 Java。 Oracle 的安装程序将 Java 版本放置在 C:\Program Files\Java\ (或 C:\Program Files (x86)\Java\)下。使用文件资源管理器或命令提示符,导航到该目录。

每个子文件夹代表一个 Java 版本。如果只有一个,那么您已经找到了。否则,请选择看起来像较新版本的版本。确保文件夹名称以 jdk 开头(而不是 jre)。输入目录。

然后进入该目录的bin目录。

您现在位于正确的目录中。复制路径。如果在文件资源管理器中,请单击地址栏。如果在命令提示符中,复制提示。

生成的 Java 路径应采用以下形式(不带引号):

C:\Program Files\Java\jdkxxxx\bin\

Zip 文件

您已经下载了包含 JDK 的 .zip 文件。将其提取到某个不会妨碍您的随机位置; C:\Java\ 是一个可以接受的选择。

然后找到其中的 bin 文件夹。

您现在位于正确的目录中。复制其路径。这是Java 路径。

请记住切勿移动该文件夹,因为这会使路径无效。

打开设置对话框

这是编辑 PATH 的对话框。有多种方法可以访问该对话框,具体取决于您的 Windows 版本、UI 设置以及系统配置的混乱程度。

尝试以下一些操作:

  • 开始菜单/任务栏搜索框 » 搜索“环境变量”
  • Win + R » control sysdm.cpl,,3
  • Win + R » SystemPropertiesAdvanced.exe » 环境变量
  • 文件资源管理器 » 在地址栏中输入 控制面板\系统和安全\系统 » 高级系统设置(最左侧,在边栏中) » 环境变量
  • 桌面 » 右键单击​​此电脑 » 属性 » 高级系统设置»环境变量
  • 开始菜单 » 右键单击​​计算机 » 属性 » 高级系统设置 » 环境变量
  • 控制面板(图标模式) » 系统 » 高级系统设置 » 环境变量
  • 控制面板(类别模式) » 系统和安全 » 系统 » 高级系统设置 » 环境变量
  • 桌面 » 右键单击​​我的电脑 » 高级 » 环境变量
  • 控制面板 » 系统 » 高级 » 环境变量

其中任何一个都应该将您带到正确的设置对话框。

如果您使用的是 Windows 10,Microsoft 已为您提供了 精美的新 UI 用于编辑 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 上执行一般内务管理的好机会。如果您的电脑上不再安装与软件相关的路径,则可以将其删除。您还可以打乱路径的顺序(如果您关心类似的事情)。

添加到 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,也请关闭并重新打开它。

另请参阅

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

过期以后 2024-12-15 20:43:16

试试这个..
我也遇到过,不过现在XP下解决了

C:\ YourFolder >set path=C:\Program Files\Java\jdk1.7.0_09\bin;
C:\ YourFolder >javac YourCode.java

try this..
I had it too but now it solved in XP..

C:\ YourFolder >set path=C:\Program Files\Java\jdk1.7.0_09\bin;
C:\ YourFolder >javac YourCode.java
隔纱相望 2024-12-15 20:43:16

您输错了 set 命令 - 您错过了 C: 后面的反斜杠。应该是:

C:\>set path=C:\Program Files (x86)\Java\jdk1.7.0\bin

You mistyped the set command – you missed the backslash after C:. It should be:

C:\>set path=C:\Program Files (x86)\Java\jdk1.7.0\bin
霞映澄塘 2024-12-15 20:43:16

从您已安装的 Java 版本的命令提示符中运行以下命令:

set Path="C:\Program Files\Java\jdk1.7.0_09\bin"

或者

set PATH="C:\Program Files\Java\jdk1.7.0_09\bin"

我已经尝试过此操作,效果很好。

Run the following from the command prompt for the version of Java you have installed:

set Path="C:\Program Files\Java\jdk1.7.0_09\bin"

or

set PATH="C:\Program Files\Java\jdk1.7.0_09\bin"

I have tried this and it works well.

预谋 2024-12-15 20:43:16

如果 java 命令正常工作并且 javac 出现问题。那么首先检查jdk的bin目录下javac.exe文件有没有。
如果 javac.exe 文件存在,则将 JAVA_HOME 设置为系统变量。

If java command is working and getting problem with javac. then first check in jdk's bin directory javac.exe file is there or not.
If javac.exe file is exist then set JAVA_HOME as System variable.

熊抱啵儿 2024-12-15 20:43:16

检查您的环境变量。

就我而言,我在系统变量用户帐户变量中设置了JAVA_HOME,而后者被设置为错误的Java版本。我的 Path 变量也遇到了同样的问题。

从我的用户帐户变量中删除 JAVA_HOME 并从 Path 变量中删除错误的路径后,它可以正常工作。

Check your environment variables.

In my case I had JAVA_HOME set in the System variables as well as in my User Account variables and the latter was set to a wrong version of Java. I also had the same problem with the Path variable.

After deleting JAVA_HOME from my User Account variables and removing the wrong path from the Path variable it worked correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文