Q:

Swig: How to pass "FILE" type from C++ to Java

I have a function in C++ for which I need to create a wrapper in Java. The function expects an input parameter of type FILE (from stdio). I don't believe that Swig can automatically appropriate an interface in Java without some manual engineering in the swig interface file - I might be wrong. 
Here is what I have -- 
C++ (theHeader.h): 
#include <cstdio>

class SampleClass 
{
 public:
  SampleClass(FILE* file) : file(file)
  {
  }

  private:
  FILE* file;
};

I have tried compiling, but it results in the creation of Swig pointer type: SWIGTYPE_p_FILE, which doesn't help me in passing the FILE type argument from Java to C++. 

A:

Why use SWIG for this?  This is pretty trivial to do directly with JNI (assuming pointers fit into jlong values for your system's architecture, which is usually a pretty safe assumption):
Java:
// get an instance of the native C++ class
static native long getClassInstance();

static native long getClassInstance( long file );

// get the native FILE * from the class instance
static native long getFileFromClass( long cls );

// close the FILE *
static native void closeFile( long file );

You'd use it like this:
long file = 0;
long cls = getClassInstance();
if ( cls != 0 )
{
    file = getFileFromClass( cls );
}

Or, if you already have the FILE * from native code:
long instance = getClassInstance( file );

Native C++ code would look something like this (I'm not bothering to do the full javac work from the above Java code):
JNIEXPORT jlong JNICALL some_class_getClassInstance(
    JNIenv *env, jclass cl )
{
    myClass *cls = new MyClass();
    return( ( jlong )( intptr_t ) cls );
}

JNIEXPORT jlong JNICALL some_class_getClassInstance(
    JNIenv *env, jclass cl, jlong file )
{
    FILE *fp = ( FILE * )( intptr_t ) file;
    myClass *cls = new MyClass( fp );
    return( ( jlong )( intptr_t ) cls );
}

JNIEXPORT jlong JNICALL some_class_getFileFromClass(
    JNIenv *env, jclass cl, jlong cls )
{
    myClass *instance = ( myClass * )( intptr_t ) cls;

    FILE *fp = myClass->getFile();

    return( ( jlong )( intptr_t ) fp );
}

JNIEXPORT void JNICALL some_class_name_closeFile(
    JNIenv *env, jclass cls, jlong file )
{
    FILE *fp = ( FILE * ) ( intptr_t ) file;
    fclose( fp );
}

Note that the code assumes that the native definition of NULL is always a zero value.  Technically, JNI makes a C call, not a C++ call, so NULL might not be guaranteed to always be actually defined to be zero, depending on your architecture and how you code up your interface between Java and native code.  So to be fully standard compliant, a NULL pointer result would cause the jlong value returned to Java to be explicitly set to zero.  The reverse works because a zero value is defined by the C Standard to be a NULL pointer value that compares equal to the defined NULL pointer constant. (Note that there can be more than one "NULL pointer value" even though there's only one definition of NULL itself.)

