C++ Code:
#include<iostream>
using namespace std;int main()
{
cout<<"Please input a Integer:\n";
int c;
cin>>c;
cout<<"The integer is :"<<c<<"\n";
return 0;
}Java Code:
public class RuntimeT { public static void runCommand(String cmd) throws IOException
{
   Process proc = Runtime.getRuntime().exec(cmd);
   InputStream istr=proc.getInputStream();
           BufferedReader br=new BufferedReader(new InputStreamReader(istr));
             String str;
while((str=br.readLine())!=null)
System.out.println(str);
       try{
              proc.waitFor();


}catch(InterruptedException e)//for waitFor()
{
System.err.println("Process was interrupted");
}
           if(proc.exitValue()!=0)
{
System.out.println(proc.exitValue());
System.err.println("Exit value was non-zero");
}

br.close();


}
       public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
try
{
runCommand("E:/C++ project/ForJava/debug/ForJava");

}
catch(IOException e)
{
System.err.println(e.getMessage());
}

}}怎么从java的console上输入数据让C++程序接收到并且打印到java的Console上。

解决方案 »

  1.   

    用java是可以调用C函数的,也可以调用exe,问题是你要从java里输入,还要从java输出,所以只能在java里调用C函数,用javah生成头文件,并且将c编译成动态连接库
    [url=http://www.10zhizui.cn][/url
      

  2.   

    今天看到bao110908 的一个帖子,深受启发,对你很有用处的,转贴一下
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;public class PingTest {    public static void main(String[] args) throws IOException {
            String url = "www.baidu.com";
            // 方法一
             System.out.println("方法一");
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("ping " + url);
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String str = "";
            while((str = br.readLine()) != null) {
                str = str.trim();
                if(str.length() > 0) {
                    System.out.println(str);
                }
            }
            br.close();        // 方法二
             System.out.println("方法二");
            URL u = new URL("http://" + url);
            try {
                HttpURLConnection http = (HttpURLConnection)u.openConnection();
                if(http.getResponseCode() != 200) {
                    System.out.println(url + " connection error.");
                }else{
                    System.out.println(url + " connection success.");
                }
            }catch(Exception e) {
                System.out.println(url + " connection error.");
            }
        }
    }这个程序调用了PING,换成你的C写的exe是一样的
      

  3.   

    嗯,你可以采用exe来直接调用,但是这样会比较慢。由于需要输入,我上面的那个方法并不适合。另外,也可以写个C++函数,编译成DLL,使用JNI方式来调用,这样会比EXE调用快很多的。
      

  4.   

    关于JNI具体地使用,可以看看IBM developerWorks上面的教程:http://www.ibm.com/developerworks/cn/views/java/tutorials.jsp?cv_doc_id=84972需要注册一个IBM账号。
      

  5.   

    BufferedReader consolein=new BufferedReader(new InputStreamReader(System.in));
    BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
        String str=consolein.readLine();
    bw.write(str);
      

  6.   

    java.lang.RunTime对象的exec方法可以执行exe文件(新建了一个进程),可以将这个进程作为输入流的源,在控制台输出。