【问题标题】:C sharp calls Java using cmdC sharp 使用 cmd 调用 Java
【发布时间】:2013-04-26 10:24:45
【问题描述】:

我正在尝试编写一个 c sharp 程序来调用 jar 文件。

我已经搜索了很多关于这类问题的解决方案,但我就是想不通。

这是我的情况,在真正调用我的 jar 文件之前,我尝试调用 java -version 进行测试。

这是一个使用参数 cmd 执行命令行的方法

public static string StartCmdProcess(string[] cmd)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.AutoFlush = true;
            for (int i = 0; i < cmd.Length; i++)
            {
                p.StandardInput.WriteLine(cmd[i].ToString());
            }
            p.StandardInput.WriteLine("exit");
            string strRst = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            p.Close();
            return strRst;
        } 

在我的主要方法中

我打电话

string result = StartCmdProcess(new string[] { "java -version" });
Console.WriteLine(result);

当我调用“java”时,它可以正常工作并输出应该打印的所有内容。

但是,当我提出 java -version 之类的参数时,它就不起作用了。

有人知道这个问题有什么问题吗?

如果有人能提供帮助,不胜感激:)

编辑:调用的结果实际上是StandardError而不是StandardOutput,但这是相当连贯的。有人知道为什么吗?

public static string StartCmdProcess(string[] cmd)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.AutoFlush = true;
            for (int i = 0; i < cmd.Length; i++)
            {
                p.StandardInput.WriteLine(cmd[i].ToString());
            }
            p.StandardInput.WriteLine("exit");
            strRst.Append(p.StandardOutput.ReadToEnd()).Append(p.StandardError.ReadToEnd());
            p.WaitForExit();
            p.Close();
            return strRst.ToString();
        }

【问题讨论】:

  • 实际上,这个调用是有效的,但是结果是 StandardError 而不是 StandardOutput。有谁知道为什么?

标签: c# java call


【解决方案1】:

您需要将参数设置为与命令分开。

 p.StartInfo.FileName = "java";
 p.StartInfo.Arguments = "-version";

当我对此进行测试时,输出被重定向,但它来自标准错误而不是标准输出。不确定为什么,但您可以像这样返回两个流:

 StringBuilder strRst = new StringBuilder();
 strRst.AppendLine(p.StandardOutput.ReadToEnd());
 strRst.AppendLine(p.StandardError.ReadToEnd());
 p.WaitForExit();
 p.Close();
 return strRst.ToString();

【讨论】:

  • 感谢您的回复!我也对其进行了测试,结果是 StandardError 而不是 StandardOutput。它确实是有线的,但至少我知道它有效。再次感谢您。
【解决方案2】:

您需要修改方法以使用 cmd 参数,如下所示

public static string StartCmdProcess(string[] cmd)
{
    System.Diagnostics.Process p = new System.Diagnostics.Process
    {
        StartInfo =
        {
            FileName = cmd[0],
            Arguments = cmd.Length>1?cmd[1]:"",
            UseShellExecute = false,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true,
        }
     };
     p.Start();
     p.StandardInput.AutoFlush = true;
     for (int i = 0; i < cmd.Length; i++)
     {
         p.StandardInput.WriteLine(cmd[i].ToString());
     }
     p.StandardInput.WriteLine("exit");
     string strRst = p.StandardOutput.ReadToEnd();
     p.WaitForExit();
     p.Close();
     return strRst;
 } 

【讨论】:

  • 感谢您的回复。使用 cmd 从 c sharp 调用“java -version”的结果是 StandardError 而不是 StandardOutput。它是有线的。
  • 你能用新代码编辑问题中的代码块吗?您可以将其添加到由 EDIT 文本分隔的现有问题中
猜你喜欢
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-27
  • 1970-01-01
相关资源
最近更新 更多