从Java程序编译C code [英] Compiling C code from a Java program

查看:199
本文介绍了从Java程序编译C code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下面的code编译外部C程​​序与Java程序

I am trying the following code to compile an external C program with a Java program

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public static void main(String[] args){
    try{
        Runtime rt=Runtime.getRuntime();
        Process pr=rt.exec("cmd /c PATH=%PATH%;c:\\TC\\BIN");
        pr=rt.exec("cmd /c c:\\TC\\BIN\\TCC.exe c:\\TC\\EXAMPLE.c");
        pr=rt.exec("c:\\TC\\EXAMPLE.exe");
        BufferedReader input=new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line=null;

        while((line=input.readLine())!=null){
            System.out.println(line);
        }
        int exitVal=pr.waitFor();
        System.out.println("exited with error code "+exitVal);
    }
    catch(Exception e){
        System.out.println(e.toString());
        //e.printStackTrace();
    }
}
}

但我正在逐渐:

java.io.IOException异常:不能运行程序C:\\ TC \\ EXAMPLE.exe:CreateProcess的错误= 2,系统找不到指定文件

java.io.IOException: Cannot run program "c:\TC\EXAMPLE.exe": CreateProcess error=2, The system cannot find the file specified

编译过程不工作,那么还有什么可以做些什么来编译我的C code?

The compilation process is not working, so what else can I do to compile my C code?

推荐答案

请使用的的ProcessBuilder 这个API,该文件对如何使用各种标志的例子。

Please use the Processbuilder API for this, The documentation has an example of how to use the various flags.

这篇关于从Java程序编译C code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆