12
votes

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.

10
Have you set %JAVA_HOME%/bin in PATH?Rahul Bobhate
You need to add Java to your system path.MD Sayem Ahmed

10 Answers

6
votes
  • 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.
6
votes

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.

3
votes

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.

2
votes
1
votes

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.

1
votes

Add it to C:/Program Files/Java/jre7/bin added to the classpath environment variable.

1
votes

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.

1
votes

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

0
votes

If it works in the regular shell but not in PowerShell, maybe you had PowerShell open when you installed or set your path and you just need to close PowerShell and reopen a new one to pick up the new path.

0
votes

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