try { final long timeout = 3000; // 限制的执行时间(毫秒) String cmd = "g++ *.cpp -o your_app_name"; final long starttime = System.currentTimeMillis(); final Process process = Runtime.getRuntime().exec(cmd); // 执行编译指令 if (process != null) { InputStream is = process.getInputStream(); // 获取编译命令输出 InputStream error = process.getErrorStream(); // 获取编译命令错误输出 new Thread() { public void run() { while (true) { try { sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } if (System.currentTimeMillis() - starttime > timeout) { // 超时 process.destroy(); } } } }.start(); } } catch (IOException e) { e.printStackTrace(); }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/229287.html原文链接:https://javaforall.net
