Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Knowing more about the natives (Applets)

Custom Search
Knowing more about the natives (Applets)

[ Return to Shlurrrpp......Java ]




Know more about the native codes

Know more about the native codes . Try out these programs and if you want to refer to the basics move to the previous page.

 


Program 1

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

Program 2

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
");
	return 1;
}

sss.def

LIBRARY     sss
EXPORTS
        Java_bbb_xx_stub    

Program 3

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
",sizeof(stack_item));
        bbb_xx(z->p);
	return z;
}

long  bbb_xx(struct yyy * t)
{
	printf("in bbb .. xx - Hi
");
        printf("Value of x %ld
",t->o->x);
        printf("Value of y %ld
",t->o->y);
	return 1;
}

sss.def
LIBRARY     sss
EXPORTS
        Java_bbb_xx_stub
In the Options Tools Directory

D:JavaInclude

D:JavaIncludeWin32

Program 4

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
");
        printf("Value of x %ld
",t->o->x);
        printf("Value of y %ld
",t->o->y);
        t->o->x = 200;
	return 1;
}

sss.def

LIBRARY     sss
EXPORTS
        Java_bbb_xx_stub    

Program 5

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
");
        printf("Value of x %ld
",t->o->x);
        printf("Value of y %ld
",t->o->y);
	return 200;
}

sss.def
LIBRARY     sss
EXPORTS
        Java_bbb_xx_stub       

Program 6

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
");
	printf("Value of u - %d
",u);
	return 1;
}

sss.def
LIBRARY     sss
EXPORTS
        Java_bbb_xx_stub       

Program 7

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
");
	printf("Value of u-%d..v-%d
",u,v);
        t->o->x=u;
        t->o->y=v;
	return 1;
}

sss.def
LIBRARY     sss
EXPORTS
        Java_bbb_xx_stub    


Program 8

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
");
        t->o->f = g;
        printf("File handle g=%d...
",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
",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


Program 9

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
");
    printf (" Name... %s
",t->m->c->name);
    printf ("Super Name.....%s
",t->m->c->super_name);
    printf("Source Name ...%s
",t->m->c->source_name);
    printf ("Methods count ..%d
",t->m->c->methods_count);
    printf ("Fields count....%d
",t->m->c->fields_count);       
    printf ("Access .....%d
",t->m->c->access);
    printf("Flags....%d
",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

Program 10

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
#include sss.def
LIBRARY     sss
EXPORTS
        Java_bbb_xx_stub       


Program 11

The above programs have been simplified for better understanding. But the programs can be created following these steps too

.

 

Step 1:

Create the file aaa.java with the following code

      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");
	       }
	}

Step 2:javac aaa.java

        Output          
			aaa.class
			bbb.class
 

Step 3:javah aaa

        Output          Error:
                        java.lang.Object not found:aborting

Step 4:set CLASSPATH=d:javalibclasses.zip

Step 5:javah aaa

        Output          Error:
                        aaa: no such class

Step 6:set CLASSPATH=..;d:javalibclasses.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
#endif

Step 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
#endif

Step 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:

Create a file called sss.c
        void bbb_xx(void *a)
        {
                printf ("In File sss - bbb-xx
");
        }

Project Files should be aaa.c, bbb.c, sss.c Build a DLL Copy the DLL from the WinDebug directory to the current directory.

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.

 

Back to the Java Page


 

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



[ Return to Shlurrrpp......Java ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy