我正在开发web-app,我正在使用Runtime类来创建执行" javac" java文件的命令。 我使用的代码是:
try{
String []cmdcom={"cmd.exe","cd..","javac",javafile};
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmdcom);
String line=null;
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
line= br.readLine();
StringBuffer sb =new StringBuffer();
while(line!=null)
{
sb.append(line);
}
compileoutput=sb.toString();
System.out.println(compileoutput);
}
cd命令用于进入我的java文件的前一个目录,即" javafile"存储。
所以,我在cmdcom数组中遇到问题,我该怎么重写这个命令?
我使用的执行代码是:
try{
ProcessBuilder pb =new ProcessBuilder("java -cp . ",request.getParameter("filename"));
pb=pb.directory(fl.getParentFile());
Process p = pb.start();
//Process p=Runtime.getRuntime().exec("java -cp . "+request.getParameter("filename"));
p.waitFor();
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
StringBuffer sb=new StringBuffer();
while((line=br.readLine())!=null)
{
sb.append(line+"\n");
}
line=null;
line=sb.toString();
Outmessage=Outmessage.concat(line);
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}