I am working on web-app and i am using Runtime class to create process for executing the "javac" command for java files. The code i used is :
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);
}
The cd command is used for going into the previous directory where my java file i.e "javafile" is stored.
So,I am having problem in the cmdcom array,how should i rewrite this command?
The execution code I used is:
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());
}