c#调用cmd使用javac没法生产class文件
c#调用cmd使用javac无法生产class文件
如题
没法生产 h1.class
希望大侠帮解决
------解决方案--------------------
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class Compile {
private String mess = null;
private File file;
private Runtime run;
private Process pro;
public static void main(String[] args) throws InterruptedException{
Compile com = new Compile();
com.setFile(new File("D:\\Test\\Java23"));
System.out.println(com.isCompile());
}
public Compile() {
this.run = Runtime.getRuntime();
}
public void setFile(File file) {
this.file = file;
}
public String isCompile() throws InterruptedException {
try {
System.out.println(this.file.getPath());
this.pro = this.run.exec("cmd.exe /c javac " + this.file.getPath() + ".java");
BufferedReader buf = new BufferedReader(new InputStreamReader(this.pro.getErrorStream()));
String str;
while ((str = buf.readLine()) != null) {
mess += str + "\n";
}
if (null == this.mess) {
this.mess = "编译成功";
}
return mess;
} catch (IOException ex) {
return ex.getMessage();
}
}
}
如题
Process p = new Process(); //创建Process对象
p.StartInfo.FileName = "cmd.exe"; //要调用的程序
p.StartInfo.UseShellExecute = false; //关闭Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向标准输入
p.StartInfo.RedirectStandardOutput = true; //重定向标准输出
p.StartInfo.RedirectStandardError = true; //重定向错误输出
p.StartInfo.CreateNoWindow = true; //设置不显示窗口
p.Start(); //启动进程
//p.StandardInput.WriteLine("f:"); //要执行的命令
p.StandardInput.WriteLine("javac h1.java");
p.StandardInput.WriteLine(" ");
//p.StandardInput.WriteLine("java h1");
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd(); //从输出流获取命令执行结果
StreamWriter sw = File.AppendText("f:\\UserLoginLog.txt"); //追加到文件
sw.WriteLine("时间:" + DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString());
//sw.WriteLine(strRst.Substring(160)); //从160个字符位置开始
sw.WriteLine(strRst);
sw.Flush();
sw.Close(); //关闭文件
Console.WriteLine(strRst); //DOS下输出命令执行结果
没法生产 h1.class
希望大侠帮解决
------解决方案--------------------
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class Compile {
private String mess = null;
private File file;
private Runtime run;
private Process pro;
public static void main(String[] args) throws InterruptedException{
Compile com = new Compile();
com.setFile(new File("D:\\Test\\Java23"));
System.out.println(com.isCompile());
}
public Compile() {
this.run = Runtime.getRuntime();
}
public void setFile(File file) {
this.file = file;
}
public String isCompile() throws InterruptedException {
try {
System.out.println(this.file.getPath());
this.pro = this.run.exec("cmd.exe /c javac " + this.file.getPath() + ".java");
BufferedReader buf = new BufferedReader(new InputStreamReader(this.pro.getErrorStream()));
String str;
while ((str = buf.readLine()) != null) {
mess += str + "\n";
}
if (null == this.mess) {
this.mess = "编译成功";
}
return mess;
} catch (IOException ex) {
return ex.getMessage();
}
}
}