Need help generating header files via jni

  • Thread starter catherineattriangle
  • Start date
C

catherineattriangle

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
 
G

Gordon Beaton

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
 
C

catherineattriangle

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
 
R

Roedy Green

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
 
G

Gordon Beaton

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top