Question

I am trying to call the "public static void main(String[])" method of Java class kissdb.dev.Run from C++ code. I use GCJ to compile:

c++ -c run.cpp; gcj run.o kissdb.so -lstdc++ -o run.x

But code below does not compile. Compiler says:

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’

What to do? My C++ code:

#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();
    }
}
Was it helpful?

Solution

You need to use the 'elements' template function.

elements(args)[i] = arg;

See the "Arrays" page in the manual.

OTHER TIPS

I am not 100% sure, because i am not sure how GCJ works. But the args array is already taken by Java. I think that if you use a different name for the args array, that it should work.

Edit: Now i read your post (learning GCJ), i see what you are trying todo. So, my answer is not totaly correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top