I'm looking for real world JNI examples
I've been looking around the net for quite a while on real world JNI examples. I am really getting tired of all the Hello,World examples that I keep finding. What I am looking for is how to get at C structures from Java code. I would like to build a JNI wrapper around an existing C Library but I cannot find enough good information beyond Hello,World to get me started. Does anyone have any good links to some tutorials which go beyond Hello,World using Java's Native Interface?
thankq
scooby_doo
Can try the following Links :
http://www.servertec.com/products/jenie/docs/stec/jenie/Dll.html
http://www.servertec.com/products/jenie/docs/stec/jenie/Function.html
These links will tell you how to inpliment a Dll
As per example is comcerned you can write ur own DLL ( for anything - From adding 2 numbers to screenscraping Mainframes, Just select any Dll that MS has and try to make one of ur own )
FYI ...
Here are 2 sample codes:
1.have a c DLL (MyDll) stdcall fnc as follows.
stdcall int MyFnc(int FileNo, int IOType, unsigned char * DataToWrite, unsig
ned char * DataRead);
class X {
private native int MyFunc(int fileNo, int IOType, byte[] DataToWrite, byte[]
DataRead);
public static void main(String args[]) {
X p = new X();
byte[] c= new byte[512];
byte[] c2= new byte[512];
int result = p.MyFunc(1,0,c,c2);
System.out.println(result);
}
static {
System.loadLibrary("MyDll");
}
}
2. public class DLLBridge
{
public DLLBridge() {
}
public native void RprtMain();
static{
System.loadLibrary("Reports");
}
public static void main(String[] args) {
DLLBridge dbridge = new DLLBridge();
dbridge.RprtMain();
}
}
That is the signature of the C function that you need to write in order to
implement the RprtMain() method that you declared as "native" in your Java
DLLBridge class. Assuming you have an existing function in a DLL that you
are trying to call, you will typically just write this function to call
the other function. If the other function has parameters, you will
typically use JNI functions to get the parameter values and massage them
into the form required by that existing function, and do the reverse with
the function result.
Of course, there is no requirement that your native method call
pre-written C functions. You can implement your native method any way you
want to.
I have not tested the code, just wrote down
inyoa at 2007-7-14 15:58:09 >

> What I am looking for is how to get at C structures
> from Java code. I would like to build a JNI wrapper
> around an existing C Library>
The below code is in c++ but the idea should be the same. This is the implementation of a JNI native method helloWorld. Here I am creating a c++ object and passing back the pointer to the object to java, so I can get to it again when needed. The 'TestClass' object is in another library. When I compiled the JNI dll, I linked it to the library containing TestClass. I included the TestClass header file in the HelloWorld header file.
JNIEXPORT jint JNICALL Java_HelloWorld_helloWorld(JNIEnv *env, jobject jobj, jstring s)
{
TestClass * tc = new TestClass(20, 10);
int y = reinterpret_cast<int>(tc);
return (jint)y;
}
Then, in another JNI method I pass back the pointer so I can access the same object and change its state.
JNIEXPORT jstring JNICALL Java_HelloWorld_testPointer(JNIEnv *env, jobject jobj, jint point)
{
TestClass * tc = reinterpret_cast<TestClass *>((int)point);
double x = tc->getX();
double y = tc->getY();
printf("%f %f", x, y);
}
Maybe thats what you're looking for. To call a method which is an existing dll, you will need to load that dll when creating your JNI dll, then you should be able to use any methods it contains.
Message was edited by:
csb42
Message was edited by:
csb42
csb42a at 2007-7-14 15:58:09 >
