2

In one of my powershell scripts - before starting a Java program - I am echoing the version of the java being used. The PS code looks quite harmless like so:

...
Write-Output "Java version:"
Write-Output "-------------"
java -version
...

The above snippet works perfectly, when I execute in a normal powershell window or as part of the startup script. The output is as expected:

Java version:
-------------
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)

But when I execute the exactly same inside the PowerShell ISE (which I normally like to use as it is often quite helpful and convenient when developing and debugging scripts) I get:

Java version:
-------------
java : java version "1.8.0_202"
At D:\Projects\gwtp-demo\mms-specifics\etc\powershell\define_gwtp-demo_profile.ps1:50 char:1
+ java -version
+ ~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (java version "1.8.0_202":String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)

What's wrong here? Why do I get this additional PS gibberish here? It seems as if PS (or the ISE?) is trying to somehow interpret that output (which is of course NOT what I want or expect. How would I have to call this "correctly" (in PS terms) to avoid such additional output?

1
  • It's treating the response from java -version as a command Commented Apr 11, 2020 at 13:08

1 Answer 1

3

Any command that writes to standard error will generate a remote exception in the ISE (or invoke-command). ISE creates a runspace to run commands in. For example, redirecting the standard error to $null results in no output:

java -version 2>$null

Here's a workaround:

java -version 2>&1 | select-string version

java version "1.8.0_241"

Or use get-package, which could apply to any software:

get-package '*java 8*'

Name                        Version         Source                           ProviderName
----                        -------         ------                           ------------
Java 8 Update 241           8.0.2410.7      C:\Program Files (x86)\Java\j... msi

By the way, you might want to switch to OpenJDK/AdoptOpenJDK, because of the license change.

Vscode doesn't behave like this.

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

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.