2

I got 2 projects. both are created in Netbeans IDE on Ubuntu 64 bit.

first, my .so project implements calling Java function from its c++ codes. and my console application try to call functions in .so file.

I got an error as following when I compiles and run:

/home/online0227/desktop/jvm run/dist/Debug/GNU-Linux-x86/jvm_run: symbol lookup error: ./libjvm_dll.so: undefined symbol: JNI_CreateJavaVM

How to resolve this?

I included include path to my .so project. Here is what my netbeans shows on its build output when I compile .so project:

g++    -c -g -I/home/online0227/jdk1.7.0_25_x64/include/linux -I/home/online0227/jdk1.7.0_25_x64/include -fPIC  -MMD -MP -MF build/Debug/GNU-Linux-x86/_ext/1117207477/testlib.o.d -o build/Debug/GNU-Linux-x86/_ext/1117207477/testlib.o /home/online0227/desktop/jvm\ dll/testlib.cpp

g++     -o dist/Debug/GNU-Linux-x86/libjvm_dll.so build/Debug/GNU-Linux-x86/_ext/1117207477/testlib.o  -shared -fPIC

this .so compiles very well and produces .so file.

And here is what my Netbeans shows when I compile my main console application:

g++    -c -g -I/home/online0227/jdk1.7.0_25_x64/include/linux -I/home/online0227/jdk1.7.0_25_x64/include -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp

g++     -o dist/Debug/GNU-Linux-x86/jvm_run build/Debug/GNU-Linux-x86/main.o -L/home/online0227/jdk1.7.0_25_x64/jre/lib/amd64/server -ldl -ljvm

It also compiles very well and run.

But the problem is I get an error message when I try to create Java Virtual Machine, "JNI_CreateJavaVM(&jvm, reinterpret_cast(&env), &vm_args)".

And following are all my source codes,

for .so project's testLib.h:

#ifndef TESTLIB_H
#define TESTLIB_H

class TestLib : public TestVir
{
 public:
     void init();
     int createJVM();
};

#endif

for .so project's testVir.h:

#ifndef TESTVIR H
#define TESTVIR_H

class TestVir
{
public:
  virtual void init()=0;
  virtual int createJVM()=0;
};

#endif

for .so project's testLib.cpp:

#include <iostream>
#include <jni.h>
#include <cassert>
#include "testVir.h"
#include "testLib.h"

using namespace std;
void TestLib::init()
{
   cout<<"TestLib::init: Hello World!! "<<endl ;
}

int TestLib::createJVM() {
  const int kNumOptions = 3;
  JavaVMOption options[kNumOptions] = {
    { const_cast<char*>("-Xmx128m"), NULL },
    { const_cast<char*>("-verbose:gc"), NULL },
    { const_cast<char*>("-Djava.class.path=."), NULL }
  };

  JavaVMInitArgs vm_args;
  vm_args.version = JNI_VERSION_1_6;
  vm_args.options = options;
  vm_args.nOptions = sizeof(options) / sizeof(JavaVMOption);
  assert(vm_args.nOptions == kNumOptions);

  JNIEnv* env = NULL;
  JavaVM* jvm = NULL;
  int res = JNI_CreateJavaVM(&jvm, reinterpret_cast<void**>(&env), &vm_args);
  if (res != JNI_OK) {
    std::cerr << "FAILED: JNI_CreateJavaVM " << res << std::endl;
    return -1;
  }

      jclass cls;
    jmethodID mid;
    jobject obj;
    int staticresult = 0;
    int result = 0;
    long status;

    if (status != JNI_ERR) {
        cls = env->FindClass("PWNJavaGUI");

        if (cls != 0) {
            mid = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");

            if (mid != 0) {
                env->CallStaticVoidMethod(cls, mid);

                mid = env->GetStaticMethodID(cls, "isPass", "()Z"); // public static int returnCheckBox()

                if (mid != 0) {

                    while (env->CallStaticBooleanMethod(cls, mid) == 0) {
                        // Do nothing but just wait until the user select game start button //
                    }

                } else {
                   // Log("Error : env->GetStaticMethodID for isLoop");
                    return 0;
                }

            } else {
              //  Log("Error : env->GetStaticMethodID for main");
                return 0;
            }

        } else {
          //  Log("Error : env->FindClass");
            return 0;
        }
    }

    return 1;

  jvm->DestroyJavaVM();

  return 0;
}

//Define functions with C symbols (create/destroy TestLib instance).
extern "C" TestLib* create()
{
    return new TestLib;
}
extern "C" void destroy(TestLib* Tl)
{
   delete Tl ;
}

And Lastly, Here is my main console application, main.cpp:

#include<iostream>
#include <stdio.h>
#include<dlfcn.h>
#include "testVir.h"

using namespace std;

int main()
{
    void *handle;
    handle = dlopen("./libjvm_dll.so", RTLD_LAZY);
    if (!handle)
    {
           printf("The error is %s", dlerror());
    }

    typedef TestVir* create_t();
    typedef void destroy_t(TestVir*);

    create_t* creat=(create_t*)dlsym(handle,"create");
    destroy_t* destroy=(destroy_t*)dlsym(handle,"destroy");
    if (!creat)
    {
           cout<<"The error is %s"<<dlerror();
    }
    if (!destroy)
    {
           cout<<"The error is %s"<<dlerror();
    }
    TestVir* tst = creat();
    tst->init();
    tst->createJVM();

    destroy(tst);
    return 0 ;
}
4

2 回答 2

4

Your libjvm_dll.so uses symbols from libjvm.so so it should be linked with it when you build it.

g++     -o dist/Debug/GNU-Linux-x86/libjvm_dll.so build/Debug/GNU-Linux-x86/_ext/1117207477/testlib.o  -shared -fPIC \
-L/home/online0227/jdk1.7.0_25_x64/jre/lib/amd64/server -ljvm

Entire build process:

# Build libjvm_dll.so
g++ -o libjvm_dll.so -I $JAVA_HOME/include testLib.cpp -shared -fPIC -L $JAVA_HOME/jre/lib/amd64/server -ljvm
# Build main executable 
g++ -o jvm_run main.cpp -ldl
# Run
LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/amd64/server ./jvm_run
于 2013-08-27T07:20:13.100 回答
0

Try to check out this website blog

于 2013-08-27T07:55:06.140 回答