Need help generating header files via jni

Discussion in 'Java' started by catherineattriangle@gmail.com, Jul 14, 2005.

  1. Guest

    Hi everyone,

    I am having some problems generating header files via Java. The actual
    case is more complicated, but this example explains my current
    stumbling block:

    This is the code I'm putting through javah:

    public class Demonstration
    {
    public String publicName;
    private String privateName;
    public static String publicStaticName;
    private static String privateStatucName;

    public native void method1();
    public native int method2(boolean b, byte by, char c, short s);
    public native byte[] method3(byte data[], boolean b[]);
    public native String[] method4(int num, long l, float f, double d);

    static
    {
    System.loadLibrary("Demonstration");
    }
    }


    This is the .h file that results:

    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class Demonstration */

    #ifndef _Included_Demonstration
    #define _Included_Demonstration
    #ifdef __cplusplus
    extern "C" {
    #endif
    /* Inaccessible static: publicStaticName */
    /* Inaccessible static: privateStatucName */
    /*
    * Class: Demonstration
    * Method: method1
    * Signature: ()V
    */
    JNIEXPORT void JNICALL Java_Demonstration_method1
    (JNIEnv *, jobject);

    /*
    * Class: Demonstration
    * Method: method2
    * Signature: (ZBCS)I
    */
    JNIEXPORT jint JNICALL Java_Demonstration_method2
    (JNIEnv *, jobject, jboolean, jbyte, jchar, jshort);

    /*
    * Class: Demonstration
    * Method: method3
    * Signature: ([B[Z)[B
    */
    JNIEXPORT jbyteArray JNICALL Java_Demonstration_method3
    (JNIEnv *, jobject, jbyteArray, jbooleanArray);

    /*
    * Class: Demonstration
    * Method: method4
    * Signature: (IJFD)[Ljava/lang/String;
    */
    JNIEXPORT jobjectArray JNICALL Java_Demonstration_method4
    (JNIEnv *, jobject, jint, jlong, jfloat, jdouble);

    #ifdef __cplusplus
    }
    #endif
    #endif


    Whatever happened to public String publicName and private String
    privateName? I'm at my wits' end from trying to figure this out. If
    this helps, I am using NetBeans IDE 4.1 and using the default Ant XML
    file with this added:

    <target name="-post-compile">
    <javah destdir="headers" classpath="build\classes\"
    verbose="yes">
    <class name="Demonstration"/>
    </javah>
    </target>

    If anyone could help, that would be great.

    Catherine
     
    , Jul 14, 2005
    #1
    1. Advertising

  2. On 14 Jul 2005 12:50:34 -0700, wrote:
    > I am having some problems generating header files via Java. The
    > actual case is more complicated, but this example explains my
    > current stumbling block:
    >
    > Whatever happened to public String publicName and private String
    > privateName?


    There is nothing wrong with the file. The primary purpose of the
    header is to provide you with the necessary signatures of your native
    *methods*.

    If your static fields had been final and given initial values, javah
    could have defined "convenience" constants that you could use in your
    native methods, something like this:

    #define Java_Demonstration_foo 10

    That way you could have used the value in your native code without
    having to look up the static field itself. In the case of non-final
    statics, it isn't meaningful to generate constants for use in the
    native code, and javah has reported that as comments in the header.
    Javah doesn't even consider the non-static member fields, since you
    always need to get their values from the containing objects.

    It's important to remember that your native methods are part of the
    class itself, the same way that your java methods are, and as such
    always have full access to *all* of the class' methods and fields.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jul 14, 2005
    #2
    1. Advertising

  3. Guest

    Gordon,

    Thanks very much for your answer. That clears up a few things. However,
    I have a follow up question.

    I have another file (the one because of which I'm working on all this)
    which looks something like this:



    public final class TOMSClient implements javax.jms.MessageListener,

    javax.jms.ExceptionListener{

    private static final int SourceID = 1;
    private static final int RegionID = 1;
    private static final int TimeZoneOffset = 0;
    private static SpeedForDatabase DatabaseSpeed = new
    SpeedForDatabase();
    private static EventForDatabase DatabaseEvent = new
    EventForDatabase();
    private static int ConnectionCount = 0;
    private static final int Connectioncpy = 279;
    private static java.sql.Connection connection = null;//Load the
    library
    static {
    System.loadLibrary("TOMSClientHelper");
    }

    /** Types of the requests supported by the TOMSDI server. SHOULD
    NOT BE CHANGED */
    private static final int REQUEST_TYPE_INCIDENTS = 3; //
    active events
    private static final int REQUEST_TYPE_EVENTS = 4; //
    scheduled events
    private static final int REQUEST_TYPE_ROADS = 5; // links
    definitions

    /** Names of the root elements for published messages. SHOULD NOT
    BE CHANGED */
    private static final String
    PUBLISH_TYPE_TRAVELER_INFO_RESPONSE_ROOT =
    "TravelerInformationResponse";
    private static final String
    PUBLISH_TYPE_EVENT_REPORT_MESSAGE_ROOT = "EventReportMessages";

    /**
    * A message property that will allow the server application to
    log in a
    * requestor IP address (for troubleshooting). SHOULD NOT BE
    CHANGED.
    */
    private static final String MSG_PROP_REQUESTOR_IPADDRESS =
    "RequestorIPAddress";

    /* END of restricted section */

    /** Types of TravelerInformationResponse data */
    private static final int TRAVELER_INFO_RESPONSE_TYPE_UNDEFINED
    = 0;
    private static final int TRAVELER_INFO_RESPONSE_TYPE_LINK_STATUS
    = 1;
    private static final int
    TRAVELER_INFO_RESPONSE_TYPE_SCHEDULED_EVENT = 2;

    /**
    * The singleton TOMSClient object.
    */
    private static TOMSClient m_tomsClient =
    null;

    ...............MORE MEMBERS................
    ...............SOME METHODS................
    public static native int GetIncidentDirection(String Highway, double
    lon1, double lat1,int RegionID, String direction, int nodeID1, int
    nodeID2, double lon2, double lat2);

    public static native boolean isSeriousIncident(int segmentID, int
    direction, int regionID);

    ...............A COUPLE OF PRIVATE CLASSES.........
    }


    The above code generates the following .h file:

    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class client_TOMSClient */

    #ifndef _Included_client_TOMSClient
    #define _Included_client_TOMSClient
    #ifdef __cplusplus
    extern "C" {
    #endif
    #undef client_TOMSClient_REQUEST_TYPE_INCIDENTS
    #define client_TOMSClient_REQUEST_TYPE_INCIDENTS 3L
    #undef client_TOMSClient_REQUEST_TYPE_EVENTS
    #define client_TOMSClient_REQUEST_TYPE_EVENTS 4L
    #undef client_TOMSClient_REQUEST_TYPE_ROADS
    #define client_TOMSClient_REQUEST_TYPE_ROADS 5L
    #undef client_TOMSClient_TRAVELER_INFO_RESPONSE_TYPE_UNDEFINED
    #define client_TOMSClient_TRAVELER_INFO_RESPONSE_TYPE_UNDEFINED 0L
    #undef client_TOMSClient_TRAVELER_INFO_RESPONSE_TYPE_LINK_STATUS
    #define client_TOMSClient_TRAVELER_INFO_RESPONSE_TYPE_LINK_STATUS 1L
    #undef client_TOMSClient_TRAVELER_INFO_RESPONSE_TYPE_SCHEDULED_EVENT
    #define client_TOMSClient_TRAVELER_INFO_RESPONSE_TYPE_SCHEDULED_EVENT
    2L
    /* Inaccessible static: m_tomsClient */
    #ifdef __cplusplus
    }
    #endif
    #endif

    So there are two problems with this: first and foremost, where are my
    native classes? Second, why are only some of the private static final
    members included in the .h file? (Connectioncpy isn't, for example.)
    This is less important, but maybe the two are connected?

    If you could shed some light on this, it'd be extremely helpful.

    Thanks,

    Catherine

    Gordon Beaton wrote:
    > On 14 Jul 2005 12:50:34 -0700, wrote:
    > > I am having some problems generating header files via Java. The
    > > actual case is more complicated, but this example explains my
    > > current stumbling block:
    > >
    > > Whatever happened to public String publicName and private String
    > > privateName?

    >
    > There is nothing wrong with the file. The primary purpose of the
    > header is to provide you with the necessary signatures of your native
    > *methods*.
    >
    > If your static fields had been final and given initial values, javah
    > could have defined "convenience" constants that you could use in your
    > native methods, something like this:
    >
    > #define Java_Demonstration_foo 10
    >
    > That way you could have used the value in your native code without
    > having to look up the static field itself. In the case of non-final
    > statics, it isn't meaningful to generate constants for use in the
    > native code, and javah has reported that as comments in the header.
    > Javah doesn't even consider the non-static member fields, since you
    > always need to get their values from the containing objects.
    >
    > It's important to remember that your native methods are part of the
    > class itself, the same way that your java methods are, and as such
    > always have full access to *all* of the class' methods and fields.
    >
    > /gordon
    >
    > --
    > [ do not email me copies of your followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    , Jul 14, 2005
    #3
  4. Roedy Green Guest

    On 14 Jul 2005 12:50:34 -0700, ""
    <> wrote or quoted :

    >Whatever happened to public String publicName and private String
    >privateName? I'm at my wits' end from trying to figure this out. If
    >this helps, I am using NetBeans IDE 4.1 and using the default Ant XML
    >file with this added:


    they are not native so they are not part of the JNI interface.

    You can get at them by a sort of reflection process, but it would be
    easier to just pass them as parameters to one of your native methods.


    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Jul 15, 2005
    #4
  5. On 14 Jul 2005 15:44:18 -0700, wrote:
    > So there are two problems with this: first and foremost, where are my
    > native classes?


    Methods, I think you mean here.

    > Second, why are only some of the private static final members
    > included in the .h file? (Connectioncpy isn't, for example.) This is
    > less important, but maybe the two are connected?


    Based on what you've posted, I can't really see why javah is being
    selective about this.

    I suspect one of the following:

    - after adding the missing members and method declarations to your
    code, you ran javah without recompiling the class first.

    - javah sees a different version of the classfile than you think it
    does. Do you have stray copies of TOMSClient.class that should be
    removed?

    My suggestion is to run "javap -p TOMSClient" to see what it says
    about your class. Do you see the missing items?

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jul 15, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Torsten Landschoff

    Location of JDK header files (for JNI code)

    Torsten Landschoff, Sep 10, 2005, in forum: Java
    Replies:
    13
    Views:
    1,436
    Raymond DeCampo
    Sep 14, 2005
  2. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    611
    Jack Klein
    Jul 24, 2004
  3. Replies:
    3
    Views:
    1,309
    Walter Roberson
    May 1, 2006
  4. Bit byte
    Replies:
    1
    Views:
    659
    benben
    Apr 19, 2006
  5. mlt
    Replies:
    2
    Views:
    853
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page