繁体   English   中英

从Java程序启动C ++程序

[英]Start C++ program from a java program

#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.. )
    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());
            }
        }
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM