[215] in java-interest

home help back first fref pref prev next nref lref last post

Re: Re: Native Methods Question

daemon@ATHENA.MIT.EDU (Chuck McManis)
Wed Jun 7 14:42:39 1995

Date: Wed, 7 Jun 1995 11:28:51 -0700
From: cmcmanis@scndprsn.Eng.Sun.COM (Chuck McManis)
To: java-interest@java.Eng.Sun.COM, kevinc@hppcih58.fc.hp.com
Cc: chrisk@hppcih58.fc.hp.com, detlef@hppcih58.fc.hp.com

Kevin Collins wrote:
	I was trying to figure out how this "miracle happens here" was
	occurring.  Let me ask a few more questions along this line :

    1) "NEVER ..." 
       How is native code called?  Continuing with the File example, is
       the native method executed back on the server via some type of
       invocation from the client?  I wish there was a picture of this
       in the documentation :)

I'll consider that an RFE :-)
    
       If it is not invoked on the server, is the implication that the
       code, and therefore the Java class, are only useful to browsers
       running on the same platform as the native code was compiled?

Yes.

       If native code is not somehow portable, then why/when/where
       would I want to use native code?

Because you couldn't do something any other way.

The easiest way to think of native code is as a non-portable implementation
of a class method. Before you can use native code, that code has to be
on the client somewhere, on Solaris this means that the shared library that
contains the code is in your LD_LIBRARY_PATH, on Windows NT I think it means
there is a DLL somewhere (I'm not at all familiar with the internals of the
Windows NT version.) 

    If I had separate libraries for all platforms available it would
    select the library based upon the platform of the browser?

Classes that use native code libraries are currently distributed through
an alternate channel (e.g. FTP). In a class distribution with native code
there would be some .class files and one or more .so (or DLL) files. The
user of this class would copy the classes into their class hierarchy or
add the directory where the classes were located to their CLASSPATH. (how
they do this is platform dependent, on Solaris you would just set the
environment variable CLASSPATH to include the new directory.) Presumably
if I downloaded the Solaris version of your classes you would not include
Windows NT libraries as well. You would have 'n' files (where n was the
number of platforms supported) Selection of the proper library is done
by the user who knows what platform they are running on presumably.

	If I wanted to encapsulate an existing C library what would you
	think about just creating a message-based interface to it and
	communicating with it via Java networking calls only?  Is this
	a reasonable approach?  Could it be supported by the Java
	networking classes?  Is there an obviously easier way?

I think you would pay a high price for sending network messages to your
library for very little benefit. Having recently ported yet another library
(some PD DES code) I simply did this:

class WrapperClass {
	int	x;	// global "state" for the library
	int	y;
	int	z;
	native void func1();
	native int func2(int x, int y);
}

In the wrapper I did this :

void
helper_setstate(struct HWrapperClass *x) {
   WrapperClass *foo = unhand(x);
   globalX = foo->x;
   globalY = foo->y;
   globalZ = foo->z;
}	

void
helper_getstate(struct HWrapperClass *x) {
   WrapperClass *foo = unhand(x);
    foo->x = globalX;
    foo->y = globalY;
    foo->z = globalZ;
}	

void
WrapperClass_func1(
	struct HWrapperClass *this)
{
	helper_setstate(this);
	func1();
	helper_getstate(this);
}
...	

The wrappers were easy to write once and the wrapper class was the new
Java interface.

Eventually there will be three levels of "code" :
	J-code - standard Java bytecodes that run on any Java runtime.
	M-code - J-code that has been further compiled into machine code.
		 This code is now bound to the platform where it was further
	 	 compiled however the original J-code is still portable.
	N-code - Native code, this code was written for a specific platform
	         and for a specific purpose.

The goal is to let you distribute everything as J-code since that is one of
the main features of Java. We'd like to produce M-code on platforms that
have a second stage compiler on those classes that can greatly benefit from
it (String comes to mind). Its still unclear to me (which is not to say its
unclear to other folks more closely tied to the second stage compiler) whether
second stage compilation will occur automatically or on a case by case basis.
And N-code when you either have to support legacy code or you have something
that lends itself to the extra work (a 3D library that knows about hardware
accellerators comes to mind.)

--Chuck
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post