简体   繁体   English

无法从Process.StandardOutput中读取

[英]Unable to read from Process.StandardOutput

I am trying to use a Process to check the version of Java installed using the "-version" command line parameter. 我正在尝试使用一个进程来检查使用“ -version”命令行参数安装的Java的版本。 However when I try to read the command line output from the java application I get a null value. 但是,当我尝试从Java应用程序读取命令行输出时,会得到一个空值。

My code is fairly simple: 我的代码很简单:

Process java = new Process();
java.StartInfo.RedirectStandardOutput = true;
java.StartInfo.CreateNoWindow = true;
java.StartInfo.UseShellExecute = false;
java.StartInfo.FileName = "java";
java.StartInfo.Arguments = "-version";
java.Start();

// version comes out null
string version = java.StandardOutput.ReadLine();

// There are probably better ways to extract this data but
// I want to get it working before I cross that bridge.
string versionNumber = version.Substring(16, 1);
hasJava = int.Parse(versionNumber) < 7;

java.Close();

It is probably something small and easy, but I can't see it. 它可能很小而又简单,但是我看不到。

You can read the currently installed Java version from the registry as well. 您也可以从注册表中读取当前安装的Java版本。 The following file includes the method TryGetJavaHome , which supports 32- and 64-bit virtual machines for specific vendor/installation combinations. 以下文件包含方法TryGetJavaHome ,该方法支持特定供应商/安装组合的32位和64位虚拟机。

bool TryGetJavaHome(
    RegistryView registryView,
    string vendor,
    string installation,
    out string javaHome)

Vendors: 供应商:

  • "JavaSoft" (HotSpot) “ JavaSoft”(热点)
  • "JRockit" “ JRockit”

Installations: 装置:

  • "Java Runtime Environment" “Java运行时环境”
  • "Java Development Kit" “ Java开发套件”

If you look at the code for the method, you'll find the following line which obtains the current version. 如果查看该方法的代码,则会发现以下获得当前版本的行。

object currentVersion = javaKey.GetValue("CurrentVersion");

This will return a value like "1.7" or "1.6". 这将返回类似“ 1.7”或“ 1.6”的值。 If want a more complete version number, some installations provide the following. 如果需要更完整的版本号, 某些安装程序提供以下内容。

object familyVersion6 = javaKey.GetValue("Java6FamilyVersion");
object familyVersion7 = javaKey.GetValue("Java7FamilyVersion");

For some installations, the family version is not present, but for the standard runtime installation I see the values "1.6.0_38" and "1.7.0_13". 对于某些安装,不提供家庭版本,但是对于标准运行时安装,我看到的值为“ 1.6.0_38”和“ 1.7.0_13”。

Reference: Antlr4ClassGenerationTaskInternal.cs 参考: Antlr4ClassGenerationTaskInternal.cs

If you remove java.CreateNoWindow = true; 如果删除java.CreateNoWindow = true; , it works. , 有用。

After some testing, I've found that the information you're looking for, for some reason, can be found in the standardError stream. 经过测试,我发现由于某种原因,您正在寻找的信息可以在standardError流中找到。

try the following. 尝试以下方法。

Process java = new Process();
java.StartInfo.RedirectStandardError = true;
java.StartInfo.CreateNoWindow = true;
java.StartInfo.UseShellExecute = false;
java.StartInfo.FileName = "java";
java.StartInfo.Arguments = "-version";
java.Start();

// version comes out null
string version = java.StandardError.ReadLine();

// There are probably better ways to extract this data but
// I want to get it working before I cross that bridge.
string versionNumber = version.Substring(16, 1);

java.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM