Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java在cmd中运行,但不是从PowerShell运行_Java_Powershell_Cmd_Environment Variables - Fatal编程技术网

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

Java在cmd中运行,但不是从PowerShell运行,java,powershell,cmd,environment-variables,Java,Powershell,Cmd,Environment Variables,我刚刚将java文件夹添加到我的环境变量中。例如,当我尝试从Windows命令行(cmd)执行javac HelloWorld.java和javahelloworld时,它会正常执行。但如果我打开Windows Powershell并执行相同操作,它会显示: javac : The term 'javac' is not recognized as the name of a cmdlet, function, script file, or operable program. Check t

我刚刚将java文件夹添加到我的环境变量中。例如,当我尝试从Windows命令行(cmd)执行
javac HelloWorld.java
javahelloworld
时,它会正常执行。但如果我打开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?

验证您从Powershell中的$env:path获得了什么。也来自cmd echo%path%
如果powershell路径中不包含javapath,请确保正确添加。

重新安装Java(相同版本/安装程序)为我修复了此问题。不需要重新启动。

路径中的JDK条目可能在引号中。

我也遇到了同样的问题,在PowerShell中运行
$env:Path
会得到如下输出

C:\Program Files\FooBar;“C:\ProgramFiles\Java\jdk1.8.0\U 92\bin”;C:\ProgramFiles(x86)\Example\whatever;。。。
请注意JDK条目是如何用引号括起来的,而其他条目则不是。显然,cmd可以处理这个问题,但PowerShell不能

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

  • 看起来像是重复的问题,为什么你们不在发布前搜索,我读了那个问题,这对我没什么帮助。我已将JRE文件夹添加到我的path环境变量中。请在powershell中显示从$env:path获得的内容。同样从cmd echo%path%中,我看到:
    ps中的$env:path
    不包含java文件夹,而cmd中的
    echo%path%
    包含。前一个列表要长得多,但确实包含以下内容:
    C:\ProgramData\Oracle\Java\javapath
    ,这是一个文件夹,其中包含指向jre目录中文件的快捷方式。您是安装了不同的版本还是完全相同的版本?