javac++gccgcj

How to create Java String array with GCJ?


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();
    }
}

Solution

  • You need to use the 'elements' template function.

    elements(args)[i] = arg;
    

    See the "Arrays" page in the manual.