如何在 PowerShell 中获取 Java 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18890926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the Java version in PowerShell
提问by Ivan
I'm trying to get the Java version in PowerShell. The version string is printed to stderr, so I'm trying to redirect it to stdout and assign it to a string variable.
我正在尝试在 PowerShell 中获取 Java 版本。版本字符串被打印到stderr,所以我试图将它重定向到 stdout 并将它分配给一个字符串变量。
I get the following strange error:
我收到以下奇怪的错误:
PS P:\> & java -version 2>&1
java.exe : java version "1.7.0_25"
At line:1 char:2
+ & <<<< java -version 2>&1
+ CategoryInfo : NotSpecified: (java version "1.7.0_25":String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
Call without redirection (2>&1) gives this:
没有重定向的调用 (2>&1) 给出了这个:
PS P:\> & java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
I think that Java here is irrelevant, and the same would happen for any other program printing strings to stderr.
我认为这里的 Java 无关紧要,任何其他程序将字符串打印到 stderr 也会发生同样的情况。
The PowerShell version I use is 2.0.
我使用的 PowerShell 版本是2.0。
Questions:
问题:
- How can I redirect stderr to a variable?
- Or, alternatively, how can I check the installed Java version?
- 如何将 stderr 重定向到变量?
- 或者,我如何检查已安装的 Java 版本?
Workaround
解决方法
I can run it like this:
我可以像这样运行它:
$output = & cmd /c "java -version 2>&1"
But I hate running a cmd.exe where it shouldn't be necessary.
但我讨厌在不必要的地方运行 cmd.exe。
采纳答案by CB.
One way is using WMI:
一种方法是使用WMI:
$javaver = Get-WmiObject -Class Win32_Product -Filter "Name like 'Java(TM)%'" | Select -Expand Version
Another one is redirect to a file with start-process:
另一个是使用 start-process 重定向到一个文件:
start-process java -ArgumentList "-version" -NoNewWindow -RedirectStandardError .\javaver.txt
$javaver = gc .\javaver.txt
del .\javaver.txt
And my last is:
我的最后一个是:
dir "HKLM:\SOFTWARE\JavaSoft\Java Runtime Environment" | select -expa pschildname -Last 1
Regarding how redirect stderr in this case you can do:
关于在这种情况下如何重定向 stderr,您可以执行以下操作:
$out = &"java.exe" -version 2>&1
$out[0].tostring()
回答by Allen
I have been using Get-Command
to get Java version on PowerShell 5.1.
我一直在使用Get-Command
PowerShell 5.1 获取 Java 版本。
Get-Command java | Select-Object Version
This returns an object. If you want a string instead, use:
这将返回一个对象。如果你想要一个字符串,请使用:
(Get-Command java | Select-Object -ExpandProperty Version).toString()
The outputs look like this:
输出如下所示:
PS > Get-Command java | Select-Object Version
Version
-------
8.0.1710.11
PS > Get-Command java | Select-Object -ExpandProperty Version
Major Minor Build Revision
----- ----- ----- --------
8 0 1710 11
PS > (Get-Command java | Select-Object -ExpandProperty Version).tostring()
8.0.1710.11
It worked quite well under PowerShell 5.1. I don't have a chance to test this on PowerShell 2.0.
它在 PowerShell 5.1 下运行良好。我没有机会在 PowerShell 2.0 上对此进行测试。