java - Why am I getting the output file as empty when I am redirecting the output from cmd? -
i creating java web application judging code online. taking .java file uploaded user , storing in c:\uploads. using processbuilder class, compiling , executing java file on server. while executing, redirecting output of java program output.txt. program gets compiled alright. problem occurring while executing, though output.txt file gets created, file empty.
here code execution
public static boolean executecode(int timelimit,string filename) { filename=filename.substring(0,filename.length()-5); //to remove .java string commands[]={"cmd", "/c","cd c:\\uploads && java"+filename+">output.txt"}; processbuilder p=new processbuilder(commands); p.redirecterrorstream(true); process process=null; boolean errcode=false; try { long start=system.nanotime(); process=p.start(); errcode=process.waitfor(timelimit,timeunit.milliseconds); long end=system.nanotime(); system.out.println("duration="+(end-start)); } catch (ioexception e) { e.printstacktrace(); } catch(interruptedexception e) { e.printstacktrace(); } return errcode; }
p.s. - problem didn't occur when created java program doing same thing.
if you're using jdk 1.7 can try this:
public static boolean executecode(int timelimit,string filename) { string commands[]={"cmd", "/c","cd c:\\uploads && javac " + filename}; processbuilder p=new processbuilder(commands); p.redirecterrorstream(true); file output = new file("c:\\uploads\\output.txt"); p.redirectoutput(output); ... }
Comments
Post a Comment