从java程序启动C ++程序

时间:2011-11-13 12:31:41

标签: java c++ process

#include <iostream>
#include <string>
using namespace std;

int main() {
        freopen( "input.txt", "r", stdin );     
        freopen( "output.txt", "w", stdout );
        string s;
        cin >> s;
        cout << s;  
        return 0; 
}

如何使用java程序执行上述C ++程序的a.exe文件? 我尝试使用以下,但没有生成output.txt文件。

Runtime rt = Runtime.getRuntime();
Process p = rt.exec(..filepath.. )

1 个答案:

答案 0 :(得分:0)

    import java.io.*;

    public class Test {
        public static void main(String[] args) {
            Runtime run = Runtime.getRuntime();
            try {

                Process pp=run.exec("c:\\a");
                BufferedReader in =new BufferedReader(new InputStreamReader(pp.getErrorStream()));

                //  Do your Stuff

                int exitVal = pp.waitFor();
                System.out.println("Process exitValue: " + exitVal);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }
}