无法在Windows PowerShell命令提示符中运行Java

15
我在我的电脑上安装了Java(JRE位于C:\Program Files\Java),但我无法从Windows PowerShell命令提示符中运行任何Java命令。

我电脑上已经安装了Java(JRE位于C:\Program Files\Java),但我无法通过Windows PowerShell命令提示符来运行任何Java命令。

>>> java -version
java : The term 'java' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ java
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (java:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

不过,我注意到Java确实可以工作,并且我可以在旧版的Windows命令提示符中运行Java命令。结果只有PowerShell无法运行这些命令。

我有JRE 7,正在使用Windows 8上的PowerShell。


3
你是否在“PATH”中设置了“%JAVA_HOME%/bin”? - Rahul Bobhate
你需要将Java添加到系统路径中。 - MD Sayem Ahmed
11个回答

12

进行2个环境变量更改:

  1. 添加一个JAVA_HOME变量,值为:C:\Java\JDK1.7.0_25_x64
  2. %JAVA_HOME%\bin添加到您的PATH变量的开头。

然后,打开一个全新的shell,以查看更改是否已应用。


1
谢谢,对于其他人来说,请不要错过步骤2,其中bin非常重要。它起作用了。 - swapyonubuntu

6
  • In the run dialog box, enter sysdm.cpl
  • In the window that came up, navigate to Advanced tab. Click on the Environment Variables button.
  • Add the location of your java installation (the bin directory) to the PATH variable.

    ;%ProgramFiles%/Java/jre%version%/bin
    

    followed by a ;

注意:
%version% means the java version.

3

您需要修改“PATH”环境变量,以添加您的“java”二进制文件位置,一种方式是通过命令提示符

set path=%path%;<您的java二进制文件位置>;

但这将仅对您终端的当前会话设置。


2

1
如果环境变量已经设置或者你已经正确地引用了环境,那么问题就是如何在Shell中执行文件。
javac .\HelloWorld.java      //That is a file, so it can be ./

java HelloWorld              //Not the correct file name, so it will not execute, and so must call it as in MSDOS

你不能在HelloWorld.class上使用.\,因为Java字节码必须在没有扩展的情况下执行,.\是用于文件执行的。

1
将其添加到 C:/Program Files/Java/jre7/bin 并将其加入类路径环境变量中。

1
谢谢。我需要在我的 Windows 10 系统中使用反斜杠。另外,由于您发布文章的时间已经过去了一段时间,对于我来说路径有一些不同。 - CaptainGenesisX
你确定需要 /bin 文件夹而不是 /lib 吗?参考链接 - Cadoiz

1
我通过重新安装Git-2.8.2-64-bit.exe并按照这些说明进行操作解决了此问题! https://learn.adafruit.com/windows-tools-for-the-electrical-engineer/git-plus-command-line-tools 然后,我添加了上述代码c:\Program Files\Java\jdk-11.0.3\bin
  • 通过导航到控制面板/system/Advanced选项卡
  • 单击“环境变量”,然后“系统变量”
  • 寻找Path以编辑
  • 然后添加c:\Program Files\Java\jdk-11.0.3\bin
之后,在Windows PowerShell和MinGW64中,一切都正常工作!看到它工作的图像:Windows power shell

很遗憾,编辑队列似乎已满 - 当路径格式化为代码 C:\my_example\path 时更易读。 - Cadoiz

1
如果在cmd中可以运行但在PowerShell中无法运行,你的JDK路径可能被引号包围
另一个问题的答案所述,进入系统变量,编辑Path的原始文本,然后删除引号即可。

0
如果在常规 shell 中可以运行但在 PowerShell 中无法运行,可能是因为您在安装或设置路径时已经打开了 PowerShell,您只需要关闭 PowerShell 并重新打开一个新的 PowerShell 即可获取新的路径。

0

我的问题是我必须重新启动终端,之后它就可以工作了。

希望这能帮助到有同样问题的人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接