Java not recognized on PowerShell

11,487

Solution 1

I deleted all the java commands from System32

This is why Windows can't find java.exe. The default JRE installation puts Java into your System32 directory, which is where CMD and Powershell usually find it.

You can fix this for your system by running the following from an admin shell. This creates a copy of java.exe in your Windows directory. (You can also probably get away with a soft link)

fsutil hardlink create (join-path $env:SystemRoot 'java.exe') (join-path $env:JAVA_HOME 'bin\java.exe')

If you don't want to modify your Windows directory (or can't), you can always set an alias to use in your Powershell session.

Set-Alias -Name java -Value (Join-Path $env:JAVA_HOME 'bin\java.exe')

Run that line in your current session and running java from the command line should work correctly. Add it to your $PROFILE if you want it to work from all future Powershell sessions.

Solution 2

  • PATH needs to point at the bin/ directory within the JDK/JRE installation
  • JAVA_HOME needs to point to the top level directory of the JDK/JRE

I suspect you're setting PATH to be the JDK/JRE folder which doesn't contain the java executable, as it's in the bin/ subdirectory...

Solution 3

1) My default JAVA_HOME is:

echo %JAVA_HOME%
D:\Program Files\Java\jdk1.7.0_25
<= The installer automagically configures this when you install a JRE

2) My default %PATH% does not include any Java

3) I'm able to run (but not compile) Java from a Windows command prompt:

java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

4) I'm also able to do exactly the same from inside Powershell (or from a .ps1 Powershell script):

PS D:\temp> java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
Share:
11,487
blueseal
Author by

blueseal

Updated on June 08, 2022

Comments

  • blueseal
    blueseal almost 2 years

    I'm using PowerShell on Windows 2012 server, and I deleted all the java commands from System32, reinstalled jdk, set JAVA_HOME and Path to point at the new installation. And I still get the following error:

    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