简体   繁体   中英

Cannot run Java from the Windows PowerShell command prompt

I have Java installed on my machine (the JRE is under C:\Program Files\Java ) but I can't run any java command from the Windows PowerShell command prompt.

>>> 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

However, I have noticed that Java does work and that I can run Java commands on the old Windows command prompt. Turns out it's only PowerShell where I can't run those commands.

I have JRE 7 and I'm using Windows PowerShell on Windows 8.

  • 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 ;

Note:

%version% means the java version.

Make 2 environment variable changes:

  1. Add a JAVA_HOME variable equal to something like: C:\\Java\\JDK1.7.0_25_x64
  2. Add %JAVA_HOME%\\bin to the beginning of your PATH variable.

Then, start a brand new shell to see if the changes were applied.

You need to modify your "PATH" environment variable to add your "java" bin location, one way via command prompt set path=%path%;<your java bin location>;

But this will be set only for your current session of your terminal.

You need to set the Java class path:

Reference:

How to set class path?

Or:

Link-2: How to set class path?

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

If the environment variable is set or you have sourced the environment appropriately, the issue is executing a file in a 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

You cannot .\\ on the HelloWorld.class as the Java bytecode must be executed without the extension, .\\ is for file execution.

If it works in cmd, but not in PowerShell, your JDK path entry might be in quotes .

As described in an answer to another question , go to system variables, edit the raw text of Path and simply remove the quotes.

I fixed this problem by reinstalling Git-2.8.2-64-bit.exe and following these instructions! https://learn.adafruit.com/windows-tools-for-the-electrical-engineer/git-plus-command-line-tools

I then added the code mentioned above c:\\Program Files\\Java\\jdk-11.0.3\\bin

  • by navigating to control panel /system/Advanced tab
  • Click on "Environment Variables" then "system's variables"
  • and looked for Path to edited and
  • then added c:\\Program Files\\Java\\jdk-11.0.3\\bin

After that, all is working for me now in a Windows PowerShell and also a MinGW64! See images of it working: Windows 电源外壳

如果它在常规 shell 中有效但在 PowerShell 中无效,则可能您在安装或设置路径时打开了 PowerShell,您只需要关闭 PowerShell 并重新打开一个新路径即可选择新路径。

If this occurs while running the script, edit the value for JAVA_HOME inside script.

example: [string] $java_home = "C:\\env\\java\\jdk-1.8.0.222",

My issue was that I had to restart my terminal, it worked after that.

I hope this helps to anyone with my same issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM