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上。
#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上。
解决方案 »
- 有5*5的表格,每行每列填入1到5的数字,要求每行不重复和每列数字不重复,如何设计程序?
- 哪位朋友愿意帮我解释一下下面的JAVA代码
- MySQL数据库只知道password,而不知道user,怎么办啊??
- 请问怎样遍历哈希,以及怎样知道这个哈希有几个key,谢谢
- java数组的索引可以是字符串么
- 写一个如同DOS的copy指令
- 一个初级问题,随给看一下,我是一个初学者,谢谢
- 高分相送:jbuider中有没有串口并口标准包?
- 几个基本java问题请教,多谢帮忙。
- 何处能下到 Tomcat?或其他简单易用的java server?
- 请问:关于struts2.0 服务器端验证Validator
- 关于JAVA底层原理的问题
[url=http://www.10zhizui.cn][/url
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是一样的
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
String str=consolein.readLine();
bw.write(str);