PowerShell 无法识别 Java

blu*_*eal 5 java windows powershell

我在 Windows 2012 服务器上使用 PowerShell,我从 System32 中删除了所有 java 命令,重新安装了 jdk,将 JAVA_HOME 和 Path 设置为指向新安装。而且我仍然收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

Rya*_*ose 5

我删除了System32中的所有java命令

这就是Windows找不到java.exe的原因。默认的 JRE 安装将 Java 放入 System32 目录中,CMD 和 Powershell 通常会在该目录中找到它。

您可以通过从管理 shell 运行以下命令来修复系统的此问题。这将在您的 Windows 目录中创建 java.exe 的副本。(你也可以通过软链接逃脱)

fsutil hardlink create (join-path $env:SystemRoot 'java.exe') (join-path $env:JAVA_HOME 'bin\java.exe')
Run Code Online (Sandbox Code Playgroud)

如果您不想(或不能)修改 Windows 目录,则始终可以设置要在 Powershell 会话中使用的别名。

Set-Alias -Name java -Value (Join-Path $env:JAVA_HOME 'bin\java.exe')
Run Code Online (Sandbox Code Playgroud)

在当前会话中运行该行并java从命令行运行应该可以正常工作。$PROFILE如果您希望它在未来的所有 Powershell 会话中都能正常工作,请将其添加到您的中。