title: JNI - make an instance of another C++ class
body: <p>I have an old application (C++) which I want to invoke with java via JNI.
  So I read some tutorials and the basics (call a method in C++ from Java) worked very well.</p>
  
  <p>But now my problem is I want to instance another C++ object in the method, which is used by JNI. Is this principally not possible or is there any way to do this?</p>
  
  <p>For explanation:</p>
  
  <p>This is my Java-Class helloworld.java which calls the native method 'callnative()'</p>
  
  <pre><code>public class helloworld{
    private native void callnative();
  
    public static void main(String[] args){
      new helloworld().callnative();
  
    }
  
    static {
       System.loadLibrary("helloworld");
    }
  }
  </code></pre>
  
  <p>This is the native method java_helloworld_callnative(..)</p>
  
  <pre><code>#include &lt;jni.h&gt;
  #include &lt;stdio.h&gt;
  #include "helloworld.h"
  #include "hellouniverse.h"
  
  JNIEXPORT void JNICALL Java_helloworld_callnative(JNIEnv *env,
                                                jobject obj)
  {
    printf("HelloWorld\n");
    hellouniverse *h = new hellouniverse();
    h-&gt;printHelloUniverse();
    return;
  }
  </code></pre>
  
  <p>This is the class hellouniverse</p>
  
  <pre><code>#include "hellouniverse.h"
  #include &lt;stdio.h&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  hellouniverse::hellouniverse(){ 
  }
  
  void hellouniverse::printHelloUniverse(){
    printf("HelloUniverse!!\n");
  }
  </code></pre>
  
  <p>I compiled helloworld.cpp with:</p>
  
  <p>g++ -fPIC -shared -I$JAVA_PATH/include -I$JAVA_PATH/include/linux/ -o libhelloworld.so hellworld.cpp</p>
  
  <p>and hellouniverse.cpp with:</p>
  
  <p>g++ -c -o hellouniverse.o hellouniverse.cpp </p>
  
  <p>When I run java helloworld the output is:</p>
  
  <p>HelloWorld
  java:symbol lookup error: $./libhelloworld.so: undefined symbol: _ZN13hellouniverseC1Ev</p>
  
  <p>I hope you can help me with my Problem :-)</p>