15

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.

snakile
  • 52,936
  • 62
  • 169
  • 241

11 Answers11

11

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.

djangofan
  • 28,471
  • 61
  • 196
  • 289
6
  • 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.
Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
3

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.

Sajan Chandran
  • 11,287
  • 3
  • 29
  • 38
2

You need to set the Java class path:

Reference:

How to set class path?

Or:

Link-2: How to set class path?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Butani Vijay
  • 4,181
  • 2
  • 29
  • 61
1

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

double-beep
  • 5,031
  • 17
  • 33
  • 41
Gyan
  • 1,176
  • 1
  • 12
  • 26
  • 1
    Thanks. I needed back slashes for my Windows 10 system. Also the path for me was a little different due to the time that has passed since your post. – CaptainGenesisX Dec 02 '20 at 19:16
  • sure that you need the `/bin` folder and not `/lib`? [Reference Link](https://www.roseindia.net/java/java-classpath.shtml) – Cadoiz Aug 31 '21 at 14:17
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Klunk
  • 11
  • 1
1

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.

Cedric Reichenbach
  • 8,970
  • 6
  • 54
  • 89
1

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

Dave
  • 1,964
  • 1
  • 10
  • 11
  • Unfortunately, the edit queue seems to be full - Paths are better readable when formatted as code `C:\my_example\path` – Cadoiz Aug 31 '21 at 09:03
0

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

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

Codeninja
  • 322
  • 1
  • 4
  • 18
0

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

I hope this helps to anyone with my same issue

Kevin Oswaldo
  • 478
  • 5
  • 13