Java在cmd中运行,但不在PowerShell中运行

我刚刚将java文件夹添加到我的环境变量中。 当我尝试从Windows命令行(cmd)执行javac HelloWorld.javajava HelloWorld ,它会正常执行。 但是,如果我打开Windows Powershell并执行相同操作,它会说:

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

另外,如果我在cmd中编译HelloWorld.java,则可以从Powershell运行java HelloWorld

如何从Powershell执行javac?

validation您从powershell中的$ env:path获得的内容。 同样来自cmd echo%path%如果在powershell路径中不包含javapath,请确保正确添加。

重新安装Java(相同的版本/安装程序)为我修复了这个问题。 无需重启。

PATH中的JDK条目可能在引号中。

我有同样的问题,并运行$env:Path PowerShell中的$env:Path给了我一个输出

 C:\Program Files\FooBar;"C:\Program Files\Java\jdk1.8.0_92\bin";C:\Program Files (x86)\Example\whatever;... 

请注意JDK条目如何引用,但其他条目不是。 显然,cmd可以处理这个,但PowerShell没有。

怎么修

  1. 打开系统环境变量编辑器,找到列表中的PATH变量。
  2. 单击“编辑…”,弹出一个列表编辑器。 请注意JDK条目是如何在引号中列出的,因此我们需要编辑原始文本。
  3. 单击“编辑文本…”以获取原始文本的编辑器,删除那里的引号并保存所有内容。