title: JNI and constructors
body: <p>I have a compiled library that I need to use in a project. To keep it short, it's a library for interacting with a specific piece of hardware. What I have is .a and .dll library files, for linux and windows respectively, and a bunch of C++ .h headers with all the public functions and classes described there.</p>
  
  <p>The problem is that the project needs to be in Java, so I need to write a JNI wrapper for this library, and honestly, I've never done that. But that's ok, I'm down to learn the thing.</p>
  
  <p>I've read up a bunch of documentation online, and I figured out passing variables, creating java objects from native code, etc.</p>
  
  <p>What I can't figure out, is how to work with native constructors using JNI? I have no idea what the source code of these constructors are, I only have the headers like this:</p>
  
  <pre><code>namespace RFDevice {
  
  class RFDEVICE_API RFEthernetDetector
  {
  public:
      //-----------------------------------------------------------------------------
      //  FUNCTION  RFEthernetDetector::RFEthernetDetector
      /// \brief    Default constructor of RFEthernetDetector object.
      ///           
      /// \return   void : N/A
      //-----------------------------------------------------------------------------
      RFEthernetDetector();
      RFEthernetDetector(const WORD wCustomPortNumber);
  </code></pre>
  
  <p>So basically if I was to write my program in C++ (which I can't), I would do something like</p>
  
  <pre><code>RFEthernetDetector ethernetDetector = new RFEthernerDetector(somePort);
  </code></pre>
  
  <p>and then work with that object. But... How do I do this in Java using JNI?
  I don't understand how am I supposed to create a native method for constructor, that would call the constructor from my .a library, and then have some way of working with that specific object? I know how to create java objects from native code - but the thing is I don't have any information about internal structure of the RFEthernetDetector class - only some of it's public fields and public methods.</p>
  
  <p>And I can't seem to find the right articles on the net to help me out. How do I do that?</p>
  
  <p>Update: A bit further clarification.</p>
  
  <p>I create a .java wrapper class like this:</p>
  
  <pre><code>public class RFEthernetDetector
  {
      public RFEthernetDetector(int portNumber)
      {
          Init(portNumber);
      }
  
      public native void Init(int portNumber);            // Void? Or what?
  }
  </code></pre>
  
  <p>then I compile it with -h parameter to generate JNI .h file:</p>
  
  <pre><code>/* DO NOT EDIT THIS FILE - it is machine generated */
  #include &lt;jni.h&gt;
  /* Header for class RFEthernetDetector */
  
  #ifndef _Included_RFEthernetDetector
  #define _Included_RFEthernetDetector
  #ifdef __cplusplus
  extern "C" {
  #endif
  /*
   * Class:     RFEthernetDetector
   * Method:    Init
   * Signature: (I)V
   */
  JNIEXPORT void JNICALL Java_RFEthernetDetector_Init
    (JNIEnv *, jobject, jint);
  
  #ifdef __cplusplus
  }
  #endif
  #endif
  </code></pre>
  
  <p>I then create an implementation that will call the functions from my .a library:</p>
  
  <pre><code>#include "RFEthernetDetector.h"     // auto-generated JNI header
  #include "RFEthernetDetector_native.h"  // h file that comes with the library, 
                      //contains definition of RFEthernetDetector class
  /*
   * Class:     RFEthernetDetector
   * Method:    Init
   * Signature: (I)V
   */
  JNIEXPORT void JNICALL Java_RFEthernetDetector_Init(JNIEnv *env, jobject thisObj, jint value)
  {
      RFEthernetDetector *rfeDetector = new RFEthernetDetector(value);    // constructor from the library
      // now how do I access this new object from Java?
      // if I need to later call rfDetector-&gt;doSomething() on that exact class instance?
  }
  </code></pre>