This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
TL;DR
对于有经验的读者:
C:\Program Files\Java\jdkxxxx\bin\
PATH
。删除旧的 Java 路径。PATH
中。JAVA_HOME
。欢迎!
您遇到了 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 上,有两种类型:用户环境变量和系统环境变量。您可以这样查看环境变量:
最重要的变量是
PATH
。它是一个路径列表,由;
分隔。当在 cmd 中输入命令时,将扫描列表中的每个目录以查找匹配的可执行文件。在我的计算机上,
PATH
是:当您运行
javac HelloWorld.java
、cmd 时,意识到javac
不是 内部命令,搜索系统PATH
后跟用户PATH
。它会自动输入列表中的每个目录,并检查存在 javac.com
、javac.exe
、javac.bat
等。当它找到javac
时,它就会运行它。如果不存在,则会打印'javac' is not recognize as an inside or external command, operable program or批处理文件。
您必须将Java可执行文件目录添加到
PATH
。JDK 与 JRE
(如果您已经了解这一点,请随意跳过本节。)
下载 Java 时,您可以在以下选项之间进行选择:
java
但不包含javac
。java
和 < code>javac,以及许多其他开发工具。 JDK 是 JRE 的超集。您必须确保您已经安装了 JDK。如果您只安装了 JRE,则无法执行
javac
,因为您的硬盘上没有安装 Java 编译器。检查您的 Windows 程序列表,并确保 Java 包的名称中包含“Development Kit”一词。不要使用
set
(如果您不打算这样做,请随意跳过本节。)
其他几个答案建议执行以下操作的一些变体:
不要这样做。该命令存在几个主要问题:
PATH
中删除所有其他内容,并将其替换为 Java 路径。执行此命令后,您可能会发现其他各种命令不起作用。C:\Program Files\Java\jdk1.7.0_09\bin
– 您几乎肯定有一个更新版本的 JDK,它会有一个不同的路径。PATH
仅适用当前 cmd 会话。每次打开命令提示符时,您都必须重新输入set
命令。第 1 点和第 2 点可以用这个稍微好一点的版本来解决:
但总的来说,这只是一个坏主意。
查找 Java 路径
正确的方法是从查找安装 Java 的位置开始。这取决于您安装 Java 的方式。
Exe 安装程序
您已通过运行安装程序安装了 Java。 Oracle 的安装程序将 Java 版本放置在
C:\Program Files\Java\
(或C:\Program Files (x86)\Java\
)下。使用文件资源管理器或命令提示符,导航到该目录。每个子文件夹代表一个 Java 版本。如果只有一个,那么您已经找到了。否则,请选择看起来像较新版本的版本。确保文件夹名称以
jdk
开头(而不是jre
)。输入目录。然后进入该目录的
bin
目录。您现在位于正确的目录中。复制路径。如果在文件资源管理器中,请单击地址栏。如果在命令提示符中,复制提示。
生成的 Java 路径应采用以下形式(不带引号):
Zip 文件
您已经下载了包含 JDK 的 .zip 文件。将其提取到某个不会妨碍您的随机位置;
C:\Java\
是一个可以接受的选择。然后找到其中的
bin
文件夹。您现在位于正确的目录中。复制其路径。这是Java 路径。
请记住切勿移动该文件夹,因为这会使路径无效。
打开设置对话框
这是编辑
PATH
的对话框。有多种方法可以访问该对话框,具体取决于您的 Windows 版本、UI 设置以及系统配置的混乱程度。尝试以下一些操作:
control sysdm.cpl,,3
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 构建系统 Maven 和 Gradle,如果设置不正确将会抛出错误。如果
JAVA_HOME
不存在,请将其创建为新的系统环境变量。设置为不含bin/
目录的Java目录的路径,即C:\Program Files\Java\jdkxxxx\
。请记住在升级 Java 后也要编辑
JAVA_HOME
。关闭并重新打开命令提示符
尽管您修改了
PATH
,但所有正在运行的程序(包括 cmd)只能看到旧的PATH
。这是因为所有环境变量的列表仅在程序开始执行时才复制到程序中;此后,它仅查阅缓存的副本。没有好的方法 刷新 cmd 的环境变量,因此只需关闭命令提示符并再次打开即可。如果您使用的是 IDE,也请关闭并重新打开它。
另请参阅
TL;DR
For experienced readers:
C:\Program Files\Java\jdkxxxx\bin\
PATH
. Remove old Java paths.PATH
.JAVA_HOME
.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 wherejavac.exe
is located. This is accomplished withPATH
, 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:
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:When you run
javac HelloWorld.java
, cmd, upon realizing thatjavac
is not an internal command, searches the systemPATH
followed by the userPATH
. It mechanically enters every directory in the list, and checks ifjavac.com
,javac.exe
,javac.bat
, etc. is present. When it findsjavac
, 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:
java
but notjavac
.java
andjavac
, 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:
Do not do that. There are several major problems with that command:
PATH
and replaces it with the Java path. After executing this command, you might find various other commands not working.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.PATH
only applies to the current cmd session. You will have to reenter theset
command every time you open Command Prompt.Points #1 and #2 can be solved with this slightly better version:
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\
(orC:\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 tojre
). 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):
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:
control sysdm.cpl,,3
SystemPropertiesAdvanced.exe
» Environment VariablesControl Panel\System and Security\System
» Advanced System Settings (far left, in sidebar) » Environment VariablesAny 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 seePATH
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 twoPATH
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, thenjavac
will invoke the Java 11 compiler, which will create version 55.class
files, butjava
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 inPATH
. 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 thebin/
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 oldPATH
. 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
试试这个..
我也遇到过,不过现在XP下解决了
try this..
I had it too but now it solved in XP..
您输错了
set
命令 - 您错过了C:
后面的反斜杠。应该是:You mistyped the
set
command – you missed the backslash afterC:
. It should be:从您已安装的 Java 版本的命令提示符中运行以下命令:
或者
我已经尝试过此操作,效果很好。
Run the following from the command prompt for the version of Java you have installed:
or
I have tried this and it works well.
如果 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.检查您的环境变量。
就我而言,我在系统变量和用户帐户变量中设置了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.