1

我正在尝试编写 ac 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();
        }
4

2 回答 2

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();
于 2013-04-26T10:29:09.910 回答
1

您需要修改方法以使用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;
 } 
于 2013-04-26T10:33:38.777 回答