我想寫一個C尖銳的程序來調用一個jar文件。C sharp調用Java使用CMD
我已經搜索了很多這類問題的解決方案,但我無法弄清楚。
這是我的情況,在真正調用我的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。有人知道爲什麼 – 2013-04-26 13:00:04