有一个c++的程序,已编译成.exe,
内容如下,
#include<iostream>
void main()
{
       int a,b;
       scanf("%d,%d",&a,&b);
a=a+b;
       printf("%d\n",a);
printf("hello c++");
}
如今想用java调用这个.exe文件,请高手赐教,在线等!

解决方案 »

  1.   

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;public class CallExe {
    public static void main(String[] args) {
    String text = null;
    String command = "C:/a.exe";//exe,bat文件名OR DOS命令
    try {
    Process proc = Runtime.getRuntime().exec(command);
    BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    while ((text = in.readLine())!= null) {
    System.out.println(text); //输出测试
    }
    } catch (IOException ioError) {
    ioError.printStackTrace();
    System.exit(0);
    }
    }这样就差不多了吧。
      

  2.   

    public static void main(String[] args) { 
    // 设置文件路径 
    String fileName="E:/eclipse/eclipse.exe"; 
    Runtime rt = Runtime.getRuntime(); 
    String exePath =   fileName; 
      try {
    rt.exec(exePath);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }  }
      

  3.   

    String   path   = "D:\\haha.exe"; 
    Runtime.getRuntime().exec(path);
      

  4.   

    String filepath="D:\\test\\02\\testrun\\b.exe";//b.exe为需要输出1,2并计算出2者和的简单程序。
    Process process =Runtime.getRuntime().exec(filepath);

    OutputStream os=process.getOutputStream() ;
    os.write("1,2\r\n".getBytes());//通过OutputStream输出流输入1,2,\r\n是回车
    os.close();//用完一定要关上

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()//读输入流