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

[519 byte] By [scooby_dooa] at [2007-10-2 23:20:26]
# 1
See examples in packages: http://www.simtel.net/product.php[id]93174[SiteID]simtel.net http://www.simtel.net/product.php[id]94368[SiteID]simtel.net http://www.simtel.net/product.php[id]95126[SiteID]simtel.net
vitallisa at 2007-7-14 15:58:09 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
Hi,You can see examples in the JNative sources at http://jnative.cvs.sourceforge.net/jnative/JNative/--Marc ( http://jnative.sf.net)
mdentya at 2007-7-14 15:58:09 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

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 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

hi may be these site can help you

http://lists.gnu.org/archive/html/classpath-patches/2005-02/msg00072.html

http://nuclear.ucdavis.edu/~jklay/STAR/FTPC/padmon/src/DataHandler.C

http://nuclear.ucdavis.edu/~jklay/STAR/FTPC/padmon/src/DataHandler.java

http://nuclear.ucdavis.edu/~jklay/STAR/FTPC/padmon/src/DataHandler.h

dollyna at 2007-7-14 15:58:09 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5
what you r exactly looking for string, array, int or object specify itmay be this could help http://sunsite.serc.iisc.ernet.in/virlib/java/unleash/jul32.htm
dollyna at 2007-7-14 15:58:09 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6
if this help plz reply http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part3/Chapter22/
dollyna at 2007-7-14 15:58:09 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7
this is good but i want to call existing methods which are there in the dll.my methods are without JNIEXPORT infront of method.
palthia at 2007-7-14 15:58:09 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 8
So, you should use a generic JNI wrapper like JNative !--Marc ( http://jnative.sf.net)
mdentya at 2007-7-14 15:58:09 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 9
can you please send me some sample code how to call method whihc is there in dll.thanks.
palthia at 2007-7-14 15:58:09 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 10
Hi,See the howto at http://jnative.free.fr/SPIP-v1-8-3/article.php3?id_article=4--Marc ( http://jnative.sf.net)
mdentya at 2007-7-14 15:58:09 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 11

> 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 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 12
http://www.forum.nokia.com/info/sw.nokia.com/id/cc5eb2db-b25a-4816-accb-57a0713ee412/Series_80_DP_2_0_Introduction_To_Java_Native_Interface_v1_0.zip.html
t0masbaileya at 2007-7-14 15:58:09 > top of Java-index,Java HotSpot Virtual Machine,Specifications...