我试图从 C++ 代码中调用 Java 类 Kissdb.dev.Run 的“public static void main(String[])”方法。我使用 GCJ 编译:
c++ -c run.cpp; gcj run.o kissdb.so -lstdc++ -o run.x
但是下面的代码无法编译。编译器说:
run.cpp: In function ‘int main(int, char**)’:
run.cpp:52:23: error: no match for ‘operator=’ in ‘*(args + ((unsigned int)(((unsigned int)i) * 8u))) = arg’
该怎么办?我的 C++ 代码:
#include <gcj/cni.h>
#include <java/lang/System.h>
#include <java/io/PrintStream.h>
#include <java/lang/Throwable.h>
#include <iostream>
#include "pub.h"
java::lang::String* js(const char* s) {
return JvNewStringLatin1(s);
}
int main(int argc, char *argv[]) {
using namespace std; // For cout <<
using namespace java::lang; // For System::class, Throwable
try {
JvCreateJavaVM(NULL);
JvAttachCurrentThread(NULL, NULL);
cout << "* Hello from GCJ! argc: " << argc << endl;
JArray<String *> *args =
(JArray<String *> *) JvNewObjectArray(argc, &String::class$, NULL);
// From http://gcc.gnu.org/onlinedocs/gcj/Arrays.html#Arrays
for (int i = 0; i < argc; i++) {
String* arg = JvNewStringLatin1(argv[i]);
args[i] = arg; // <--- ERROR HERE
}
kissdb::dev::Run::main(args); // The Java main method I want to run.
JvDetachCurrentThread();
} catch (Throwable *t) {
System::out->println(js("Unhandled Java exception:"));
t->printStackTrace();
}
}