1

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();
    }
}
Frans Lundberg
  • 418
  • 3
  • 8

2 Answers2

1

You need to use the 'elements' template function.

elements(args)[i] = arg;

See the "Arrays" page in the manual.

Tom Tromey
  • 21,507
  • 2
  • 45
  • 63
  • Great, now it works! Thank you so much! I have not used C++ template functions for many years. I saw the manual, but did not really get it. – Frans Lundberg Jun 07 '13 at 10:32
  • Now, I might be able to release my database, [kissdb.com](http://kissdb.com/) for use with GCJ too. KissDB is designed to make it simple, fun, and efficient to store software objects. – Frans Lundberg Jun 07 '13 at 10:34
  • I had to rename my database to BergDB, [bergdb.com](http://bergdb.com/). It now works with GCJ - have not release the GCJ version yet, however. – Frans Lundberg Aug 09 '13 at 07:23
0

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.

Dippo
  • 184
  • 2
  • 11