Мое текущее решение выглядит так:
код запуска:
<code>@RequestMapping("/student/runITask.action")
public String student_runITask(@ModelAttribute(value = "program") ProgramToCompile program, ModelMap map) {
//1. code compile
ITaskCompile itcompile = new ITaskCompile();
List<String> errorList = itcompile.compileTask(program.getClassname(), program.getProgram());
Date tmp = new Date();
this.setPathName(program.getClassname() + tmp.hashCode());
//2. if compiled...
if (errorList.size() < 1) {
try {
String[] cmd = {"/bin/sh", "-c", "java -Xmx16M -Xms2M -cp /root/ " + program.getClassname() + "> " + getPathName() + ".data"};
Runtime rt = Runtime.getRuntime();
final Process proc = rt.exec(cmd);
Thread.sleep(1000);
proc.destroy();
if (proc.exitValue() > 0) {
try {
killJavaProcesses();
map.addAttribute("comment", "Endless LOOP!");
} catch (Exception ex1) {
Logger.getLogger(CompileITaskControler.class.getName()).log(Level.SEVERE, null, ex1);
}
} else {
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader("/root/" + getPathName() + ".data"));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
fileData.append(buf, 0, numRead);
}
reader.close();
map.addAttribute("comment","Output: <br/><br/><br/><pre>"+fileData.toString()+"
");
}
} catch (Exex ex) {
пытаться {
killJavaProcesses ();
map.addAttribute ("comment", "Endless LOOP!");
} catch (Exception ex1) {
Logger.getLogger (CompileITaskControler.class.getName ()). Log (Level.SEVERE, null, ex1);
}
}
} еще {
map.addAttribute ("errorList", errorList);
map.addAttribute («комментарий», «ПРОГРАММА NIE ZOSTAŁ URUCHOMIONY»);
} // 3. вернуть
вернуть DISPLAY_COMP_MSG;
}
где killJavaProcesses () выглядит так
public void killJavaProcesses() throws IOException, InterruptedException {
String[] getProcessList = {"/bin/sh", "-c", "ps | grep java"};
String[] killProcessByIdList = {"/bin/sh", "-c", "kill -9 "};
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(getProcessList);
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
String line2;
String kill = new String();
while ((line2 = bufferedreader.readLine()) != null) {
kill += line2 + "\n";
}
proc.destroy();
String arraykill[] = kill.split("\n");
String element2kill = "";
String[] tmp;
if (arraykill.length >= 1) {
element2kill = arraykill[arraykill.length - 1].trim().split(" ")[0];
}
killProcessByIdList[2] += element2kill;
Process proc2 = rt.exec(killProcessByIdList);
proc2.waitFor();
}
Я не могу убить процесс другим. Использование proc.destroy () не работает на компьютерах с Ubuntu / Win XP.
Теперь я попытаюсь настроить и использовать SecurityManager .