Dare To Think, Strive To Execute

Java 调用C++可执行程序

如何调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.lang.ProcessBuilder;
public class TestCallForExe {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("hello world!");
try {
ProcessBuilder proc = new ProcessBuilder("/a.out", "testfile");
proc.start();
}
catch (Exception e) {
System.out.println("Error executing notepad.");
}
//System.out.println("Goodbye world!");
}
}

简单测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "iostream"
#include "fstream"
using namespace std;
int main(int argc, char *argv[]){
for (int i = 0; i < argc; i++) {
cout<<"args: "<<i<<" :"<<argv[i]<<endl;
}
ofstream f1;
f1.open("./write_test.txt");
for (int i = 0; i < argc; i++) {
f1<<"args: "<<i<<" :"<<argv[i]<<endl;
}
f1<<"hello world!"<<endl;
f1.close();
return 0;
}