有一个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文件,请高手赐教,在线等!
内容如下,
#include<iostream>
void main()
{
int a,b;
scanf("%d,%d",&a,&b);
a=a+b;
printf("%d\n",a);
printf("hello c++");
}
如今想用java调用这个.exe文件,请高手赐教,在线等!
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);
}
}这样就差不多了吧。
// 设置文件路径
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();
} }
Runtime.getRuntime().exec(path);
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()//读输入流