java编译运行cpp文件

来源:互联网 发布:安卓旧版本软件 编辑:程序博客网 时间:2024/09/21 06:37
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();}


0 0
原创粉丝点击