Compile java with C#

本文关键字:with java Compile | 更新日期: 2023-09-27 17:53:21

我想用c#编译一个Java程序。有人知道为什么我不能接受程序的输出,而我可以接受错误吗?我如何从。java打印结果?

Process p = new Process(); p.StartInfo.FileName = "C:''Program Files''Java''jdk1.7.0_04''bin''javac";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = "c:''java''upgrade.java";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();           
resultcode.Text = p.StandardOutput.ReadToEnd();

Compile java with C#

我想你的意思是"我如何从javac.exe捕获标准输出和标准错误文本?"

你已经得到了大部分的答案:

1)在Process对象中指定"redirection":

Process p = new Process(); 
p.StartInfo.FileName = @"C:'Program Files'Java'jdk1.7.0_04'bin'javac";
...
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
2)分配c# I/O对象重定向到:
StreamReader outputReader = null;
StreamReader errorReader = null;
...
outputReader = p.StandardOutput;
errorReader = p.StandardError;
3)最后,从I/O对象中读取:
string myText = "StdOut:" + Environment.NewLine;
myText += outputReader.ReadToEnd();
myText += Environment.NewLine + "Stderr:" + Environment.NewLine;
myText += errorReader.ReadToEnd();
Console.WriteLine("Complete output:" + myText);
4)最后,如果你DON'T想要打开你自己的I/O对象,那么DON'T设置p.StartInfo.RedirectStandardInput = true; .