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();
}
java编译运行cpp文件
最新推荐文章于 2024-07-17 16:48:53 发布