Know more about the native codes . Try out these programs and if you want to refer to the basics move to the previous page.
aaa.java class aaa { public static void main(String s[]) { bbb a; System.out.println("Start"); a = new bbb(); System.out.println("Before xx"); a.xx(); System.out.println("After xx"); } } class bbb { public native void xx(); public bbb() { System.out.println("in bbb"); System.loadLibrary("sss"); } } sss.c #include <windows.h> void *Java_bbb_xx_stub(void * p) { MessageBox(0,"Hi","Hi",0); return p; } sss.def LIBRARY sss EXPORTS Java_bbb_xx_stub The Files to be included in the Project are sss.c user32.lib
aaa.java class aaa { public static void main(String s[]) { bbb a; a = new bbb(); a.xx(); System.out.println("After xx"); } } class bbb { public native void xx(); public bbb() { System.out.println("in bbb Constructor"); System.loadLibrary("sss"); } } sss.c void * Java_bbb_xx_stub(void *z,void *e) { bbb_xx(); return z; } long bbb_xx() { printf("in bbb .. xx - Hi\n"); return 1; } sss.def LIBRARY sss EXPORTS Java_bbb_xx_stub
aaa.java class aaa { public static void main(String s[]) { bbb a; a = new bbb(); a.xx(); System.out.println("After xx "); } } class bbb { int a , b; public native void xx(); public bbb() { a=10; b=20; System.out.println("in bbb Constructor"); System.loadLibrary("sss"); } } sss.c #include <StubPreamble.h> struct zzz { long x,y; }; struct yyy { struct zzz *o; void * m; }; stack_item * Java_bbb_xx_stub(stack_item *z,void * e) { printf("The sizeof stack_item..%d\n",sizeof(stack_item)); bbb_xx(z->p); return z; } long bbb_xx(struct yyy * t) { printf("in bbb .. xx - Hi\n"); printf("Value of x %ld\n",t->o->x); printf("Value of y %ld\n",t->o->y); return 1; } sss.def LIBRARY sss EXPORTS Java_bbb_xx_stub In the Options Tools DirectoryD:\Java\Include
D:\Java\Include\Win32
aaa.java class aaa { public static void main(String s[]) { bbb a; a = new bbb(); System.out.println("After a..."+a.a); a.xx(); System.out.println("After a..."+a.a); } } class bbb { int a , b; public native int xx(); public bbb() { a=10; b=20; System.out.println("in bbb"); System.loadLibrary("sss"); } } sss.c #include <StubPreamble.h> struct zzz { long x,y; }; struct yyy { struct zzz *o; void *m; }; stack_item * Java_bbb_xx_stub(stack_item *z,void *e) { bbb_xx(z->p); return z+1; } long bbb_xx(struct yyy * t) { printf("in bbb .. xx - Hi\n"); printf("Value of x %ld\n",t->o->x); printf("Value of y %ld\n",t->o->y); t->o->x = 200; return 1; } sss.def LIBRARY sss EXPORTS Java_bbb_xx_stub
aaa.java class aaa { public static void main(String s[]) { int i; bbb a; a = new bbb(); i=a.xx(); System.out.println("i = Return of xx =" + i); } } class bbb { int a , b; public native int xx(); public bbb() { a=10; b=20; System.out.println("in bbb"); System.loadLibrary("sss"); } } sss.c #include <StubPreamble.h> struct zzz { long x,y; }; struct yyy { struct zzz *o; void *m; }; stack_item * Java_bbb_xx_stub(stack_item *z,void *e) { z->i=bbb_xx(z->p); return z+1; } long bbb_xx(struct yyy * t) { printf("in bbb .. xx - Hi\n"); printf("Value of x %ld\n",t->o->x); printf("Value of y %ld\n",t->o->y); return 200; } sss.def LIBRARY sss EXPORTS Java_bbb_xx_stub
aaa.java class aaa { public static void main(String s[]) { int i; bbb a; a = new bbb(); a.xx(200); System.out.println("After xx"); } } class bbb { int a , b; public native int xx(int p); public bbb() { a=10; b=20; System.out.println("in bbb"); System.loadLibrary("sss"); } } sss.c #include <StubPreamble.h> struct zzz { long x,y; }; struct yyy { struct zzz *o; void *m; }; stack_item * Java_bbb_xx_stub(stack_item *z,void *e) { bbb_xx(z->p,z[1].i); return z+1; } long bbb_xx(struct yyy * t,long u) { printf("in bbb .. xx - Hi\n"); printf("Value of u - %d\n",u); return 1; } sss.def LIBRARY sss EXPORTS Java_bbb_xx_stub
aaa.java class aaa { public static void main(String s[]) { bbb a; a = new bbb(); System.out.println("Value of a.."+a.a+"..b.."+a.b); a.xx(200,3); System.out.println("After xx"); System.out.println("Value of a.."+a.a+"..b.."+a.b); } } class bbb { int a,b; public native int xx(int p,int q); public bbb() { a=10; b = 20; System.out.println("in bbb"); System.loadLibrary("sss"); } } sss.c #include <StubPreamble.h> struct zzz { long x,y; }; struct yyy { struct zzz *o; void *m; }; stack_item * Java_bbb_xx_stub(stack_item *z,void *e) { bbb_xx(z->p,z[1].i,z[2].i); return z; } long bbb_xx(struct yyy * t,long u,long v) { printf("in bbb .. xx - Hi\n"); printf("Value of u-%d..v-%d\n",u,v); t->o->x=u; t->o->y=v; return 1; } sss.def LIBRARY sss EXPORTS Java_bbb_xx_stub
aaa.java class aaa { public static void main(String s[]) { byte t[]; bbb b; int c=0; b = new bbb("a.txt"); t = new byte[1]; b.xx(); System.out.println("After open"); while (b.yy(t,1) == 1) c++; System.out.println("No. of characters."+c); } } class bbb { String s; int d; public bbb(String p) { System.out.println("in constructor"); System.loadLibrary("sss"); s=p; } public native boolean xx(); public native int yy (byte b[], int l); } sss.c #include <StubPreamble.h> #include <fcntl.h> struct zzz { struct Hjava_lang_String * p; long f; }; struct yyy { struct zzz * o; void * m; }; long bbb_xx(struct yyy * t) { int g; char u[1000]; javaString2CString(t->o->p,u,1000); g = open(u,O_RDONLY); printf("After open\n"); t->o->f = g; printf("File handle g=%d...\n",g); return 1; } struct pp { char b[1]; }; struct qq { struct pp * o; void *m; } ; long bbb_yy(struct yyy * t,struct qq * v, long c) { char *d = v->o->b; int q = c; q = read(t->o->f,d,q); if(q == 0) return (-1); printf("No. of char q=%d...Character *d=%c\n",q,*d); return (q); } stack_item * Java_bbb_xx_stub(stack_item * z, void *e) { bbb_xx(z->p); return z + 1; } stack_item * Java_bbb_yy_stub(stack_item * z, void * e) { z->i=bbb_yy(z->p,((z[1].p)),((z[2].i))); return z + 1; } sss.def LIBRARY sss EXPORTS Java_bbb_xx_stub Java_bbb_yy_stub OR __declspec (dllexport) stack_item * Java_bbb_xx_stub(stack_item * z, void *e) { bbb_xx(z->p); return z + 1; } __declspec (dllexport) stack_item * Java_bbb_yy_stub(stack_item * z, void * e) { z->i=bbb_yy(z->p,((z[1].p)),((z[2].i))); return z + 1; } There is no need for exporting these functions in the .def file. Project Files will be sss.c javai.lib
aaa.java class aaa { public static void main(String s[]) { bbb b; b = new bbb(); b.xx(); } } class bbb { int a; public native int xx(); public bbb() { a=10; System.out.println("in bbb Constructor"); System.loadLibrary("sss"); } public void ppp() { System.out.println("In ppp"); } public void qqq() { System.out.println("In qqq"); } } sss.c #include <StubPreamble.h> struct mt { struct ClassClass *c; void *md[1]; }; struct zzz { long x; }; struct yyy { struct zzz *o; struct mt *m; }; stack_item * Java_bbb_xx_stub(stack_item *z,void *e) { bbb_xx(z->p); return z; } long bbb_xx(struct yyy * t) { printf("in bbb .. xx - Hi\n"); printf (" Name... %s\n",t->m->c->name); printf ("Super Name.....%s\n",t->m->c->super_name); printf("Source Name ...%s\n",t->m->c->source_name); printf ("Methods count ..%d\n",t->m->c->methods_count); printf ("Fields count....%d\n",t->m->c->fields_count); printf ("Access .....%d\n",t->m->c->access); printf("Flags....%d\n",t->m->c->flags); return 1; } sss.def LIBRARY sss EXPORTS Java_bbb_xx_stub Output in bbb Constructor in bbb .. xx - Hi Name...bbb Super Name.....java/lang/Object Source Name... aaa.java Methods count ..4 Fields count ....1 Access .....0 Flags....18
aaa.java class aaa { public static void main(String s[]) { bbb a; a = new bbb(); a.xx(); } } class bbb { int a; int b; public native int xx(); public bbb() { a=10; b = 30; System.out.println("in bbb"); System.loadLibrary("sss"); } public void ppp() { System.out.println("In ppp"); } public void qqq() { System.out.println("In qqq"); } } sss.c #includesss.def LIBRARY sss EXPORTS Java_bbb_xx_stub
.
Step 1:
Create the file aaa.java with the following codeclass aaa { public static void main(String s[]) { bbb a; System.out.println("Start"); a = new bbb(); System.out.println("Before xx"); a.xx(); System.out.println("After xx"); } } class bbb { public native void xx(); public bbb() { System.out.println("in bbb"); System.loadLibrary("sss"); } }Step 2:javac aaa.java
Output aaa.class bbb.classStep 3:javah aaa
Output Error: java.lang.Object not found:abortingStep 4:set CLASSPATH=d:\java\lib\classes.zip
Step 5:javah aaa
Output Error: aaa: no such classStep 6:set CLASSPATH=..;d:\java\lib\classes.zip
Step 7:javah aaa
Output aaa.h /* DO NOT EDIT THIS FILE - it is machine generated */ #include <native.h> /* Header for class aaa */ #ifndef _Included_aaa #define _Included_aaa typedef struct Classaaa { char PAD; /* ANSI C requires structures to have a least one member */ } Classaaa; HandleTo(aaa); #ifdef __cplusplus extern "C" { #endif #ifdef __cplusplus } #endif #endifStep 8:javah bbb
Output bbb.h /* DO NOT EDIT THIS FILE - it is machine generated */ #include <native.h> /* Header for class bbb */ #ifndef _Included_bbb #define _Included_bbb typedef struct Classbbb { char PAD; /* ANSI C requires structures to have a least one member */ } Classbbb; HandleTo(bbb); #ifdef __cplusplus extern "C" { #endif __declspec(dllexport) void bbb_xx(struct Hbbb *); #ifdef __cplusplus } #endif #endifStep 9:javah -stubs aaa
Output aaa.c /* DO NOT EDIT THIS FILE - it is machine generated */ #include <StubPreamble.h> /* Stubs for class aaa */Step 10:javah -stubs bbb
Output bbb.c /* DO NOT EDIT THIS FILE - it is machine generated */ #include <StubPreamble.h> /* Stubs for class bbb */ /* SYMBOL: "bbb/xx()V", Java_bbb_xx_stub */ __declspec(dllexport)stack_item*Java_bbb_xx_stub(stack_item *_P_,struct execenv *_EE_){ extern void bbb_xx(void *); (void) bbb_xx(_P_[0].p); return _P_; }Step 11:
Project Files should be aaa.c, bbb.c, sss.c Build a DLL Copy the DLL from the WinDebug directory to the current directory.Create a file called sss.c void bbb_xx(void *a) { printf ("In File sss - bbb-xx\n"); }Step 12:java aaa
Output In Start Before xx In File sss - bbb-xx After xx
Feel free to send us your comments , feedback , suggestions which are most welcomed. Tell us how we can improvise the tutorial.
Vijay Mukhi's Computer Institute
B-13, Everest Building, Tardeo, Bombay 400 034, India.
http://www.neca.com/~vmis
e-mail : vmukhi@giasbm01.vsnl.net.in