JNI with Qt part1

I work with java every day, using kde kate editor http://kate-editor.org/,
and i was wondering how nice it would be with a java autocompletation plugin.

So i started looking for a c++ library that could get information from a java class, with no results :(.

The only possibility that i could find was JNI.

Use jni to get the information directly from a java vm using java reflection.

Here is my first try with Qt4.5:

  • 1-. Create the qmake project file

    ARCH = i386
    QT       -= gui
    TARGET = sample_jni_project
    CONFIG   += console
    CONFIG   -= app_bundle
    INCLUDEPATH += $$JAVA_HOME/include
    INCLUDEPATH += $$JAVA_HOME/include/linux
    LIBS += $$JAVA_HOME/jre/lib/$$ARCH/server/libjvm.so
    TEMPLATE = app
    SOURCES += main.cpp
  • 2.- main.cpp

    // Qt headers
    #include <QDebug>
    // JNI headers
    #include <jni.h>
    const char *getPropertyValue(JNIEnv *env, const char *name)
        // Calling System.getProperty(propertyName);
        jclass systemClass = env->FindClass("java/lang/System");
        jmethodID getPropertyMethod = env->GetStaticMethodID(systemClass, "getProperty",
        jstring propertyName = env->NewStringUTF(name);
        jstring value = (jstring) env->CallStaticObjectMethod(systemClass,
        return env->GetStringUTFChars(value, NULL);
    int main(int argc, char *argv[])
        JavaVM *vm;
        JNIEnv *env;
        JavaVMInitArgs args;
        jint ret;
        args.version = JNI_VERSION_1_2;
        args.nOptions = 0;
        args.ignoreUnrecognized = JNI_FALSE;
        ret = JNI_CreateJavaVM(&vm, (void **) &env, &args);
        qDebug() << "Java version : " << QString(getPropertyValue(env, "java.version"));
        qDebug() << "Java vendor  : " << QString(getPropertyValue(env, "java.vendor"));
        return 0;

This sample only shows how to call System.getProperty static method, but it’s a start

To execute

export JAVA_HOME=/path/where/java/is
export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/i386/server


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: