我想使用C#進程從命令提示符中獲取結果。該命令是 「的Java的HelloWorld 1」(我已經建立它使用「的javac HelloWorld.java」 HelloWorld.class的文件)C#調用java.exe並從cmd中獲取結果錯誤
Java代碼:
public class HelloWorld {
public HelloWorld() {}
public static void main(String[] args) { System.out.println("STARTED");
try {
int param = Integer.parseInt(args[0].toString());
if (param == 1) {
System.out.println("BASE 64!");
} else if (param == 2) {
System.out.println("MD5!");
} else {
System.out.println("INPUT NOT MATCH!");
}
} catch (Exception ee) {
System.out.println("NO INPUT - ERROR");
}
} }
和C#代碼:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Program Files\\Java\\jdk1.6.0_25\\bin\\java.exe";
p.StartInfo.Arguments = "HelloWorld 1";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
但strOutput是「」。 你能給我解決方案嗎? 謝謝!
我使用cmd「javac HelloWorld.java」來構建HelloWorld.class,並將代碼更改爲:p.StartInfo.FileName = 「C:\\ Program Files \\ Java \\ jdk1.6.0_25 \\ bin \\ java.exe」; p.StartInfo.Arguments =「-cp C:\\ Program Files \\ Java \\ jdk1.6.0_25 \\ bin \\ HelloWorld 1」;但它不會運行 – QuangVinh 2012-03-23 08:25:36
是C:\\ Program Files \\ Java \\ jdk1.6.0_25 \\ bin \\中的HelloWorld.class。 cp選項需要指向類文件的位置。 – GregHNZ 2012-03-23 08:30:39
@QuangVinh類路徑不應該是可執行文件(java.exe),而是編譯的java代碼的位置。即HelloWorld的路徑 – 2012-03-23 08:31:00