title: Call C++ functions from Java
body: <p>This is my first post on StackOverflow. I am working on a project and I need your help in JNI. I got stuck there... I have been reading and trying it a lot recently, but still didn't figure out how to make it work.</p>
  
  <p>I have created a static library in C++ (a pseudo-code is shown below):</p>
  
  <pre><code>// file: X.h    
  class X {
      public:
          X() {};
          ~X() {};
  
          void fooX() { // do stuff };
          void barX() { // do more stuff };
  }
  
  // file: X.cpp
  #include "X.h";
  
  // file: Y.h 
  #include "X.h"
  class Y {
      public:
          Y() {x = new X()};
          ~Y() {};
  
          fooY() { // do stuff };
          barY() { // do more stuff };
  
      private:
          X x;    // object of class X
  }
  
  // file: Y.cpp
  #include "Y.h"
  
  // file: Z.h 
  #include "Y.h"
  class Z {
      public:
          Z(uint8_t, std::string, std::vector&lt;uint8_t&gt;);
          ~Z() {};
  
          fooZ() { // do stuff };
          barZ() { // do more stuff };
  
      private:
          Y y;    // object of class Y
  }
  
  // file: Z.cpp
  #include "Z.h"
  Z::Z(uint8_t a, std::string b, std::vector&lt;uint8_t&gt; c)
  {
       /// do stuff and create an object of Y
       Y y = new Y();
  }
  
  // file api.h
  #include "Z.h"
  void accessZ(uint8_t, std::string, std::vector&lt;uint8_t&gt;);
  
  // file api.cpp
  #include "api.h"
  void accessZ(uint8_t a_uint, 
       std::string b_string, 
       std::vector&lt;uint8_t&gt; c_vector)
  {
       // create object of Z
       Z z = new Z(a_uint, b_string, c_vector);
       z-&gt;fooZ();
  
       delete z;
       z = NULL; 
  }
  </code></pre>
  
  <p>Further, I have compiled all of the above code as a static library (libXYZ.a) in C++ (Eclipse C++ CDT on Windows 7 using MinGW).</p>
  
  <p>Now, what I would like to have is to be able to call the API <strong>accessZ()</strong> C++ function from a Java application!
  In other words, I have implemented the core functionality in C++ and I would like to implement the GUI in Java. Therefore, I need to access the C++ functions from the Java GUI...</p>
  
  <p><strong><em>How can I do that?</em></strong> I need direct answers and not vague ones please...</p>
  
  <p>Your help is very appreciated.</p>
  
  <p>Thanks a lot in advance for your support.</p>
  
  <p><strong>Edited</strong></p>
  
  <p>My project is as follows:</p>
  
  <pre><code>                                MyLibrary
                                      |
  Makefile        header/     source/        bin/       object/     lib/
                  X.h         X.cpp                     X.o       libXYZ.a
                  Y.h         Y.cpp                     Y.o
                  Z.h         Z.cpp                     Z.o
                  api.h       api.cpp                   api.o
  </code></pre>
  
  <p>All what I need is to be able to pass some parameters from Java to <strong>libXYZ</strong> using the <strong>accessZ()</strong> function.</p>
  
  <p><strong>New Edit:</strong></p>
  
  <pre><code>public final class NativeClass {
    {
      System.loadLibrary("myLibrary");
    }
  
    public native void accessZ(char a_uint, String b_string, char[] c_vector);
  
    public static final NativeClass getInstance() {
      return INSTANCE;
    }
  
    private static final NativeClass INSTANCE = new NativeClass();
  };
  </code></pre>