18

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.

2
  • 3
    Have you set %JAVA_HOME%/bin in PATH? Commented May 29, 2013 at 10:21
  • You need to add Java to your system path. Commented May 29, 2013 at 10:22

11 Answers 11

15

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, for others, do not miss step 2 where bin is important. It worked
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.

Comments

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.

Comments

2

You need to set the Java class path:

Reference:

How to set class path?

Or:

Link-2: How to set class path?

Comments

2

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

2 Comments

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.
sure that you need the /bin folder and not /lib? Reference Link
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.

Comments

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.

Comments

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

1 Comment

Unfortunately, the edit queue seems to be full - Paths are better readable when formatted as code C:\my_example\path
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.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.