2013-04-26 38 views
1

我想寫一個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(); 
     } 
+1

其實,這個叫作品,但結果將轉換爲StandardError而不是StandardOutput。有人知道爲什麼 – 2013-04-26 13:00:04

回答

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(); 
+0

謝謝你的回覆!我也測試過它,結果是StandardError而不是StandardOutput。這是真的有線,但至少我知道它的作品。再次感謝你。 – 2013-04-26 12:48:00

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; 
} 
+0

謝謝你的回覆。使用cmd從c sharp調用「java -version」的結果將轉換爲StandardError而不是StandardOutput。它是有線的。 – 2013-04-26 12:58:36

+0

你能用新代碼編輯問題中的代碼塊嗎?您可以將它添加到由編輯文本分隔的現有問題 – 2013-04-26 13:02:52

+0

是的,我發佈了它。 :) – 2013-04-26 13:27:15