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

2 Answers 2

2

You need to use the 'elements' template function.

elements(args)[i] = arg;

See the "Arrays" page in the manual.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
Now, I might be able to release my database, kissdb.com for use with GCJ too. KissDB is designed to make it simple, fun, and efficient to store software objects.
I had to rename my database to BergDB, bergdb.com. It now works with GCJ - have not release the GCJ version yet, however.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.