<s>
In	O
software	O
design	O
,	O
the	O
Java	B-Language
Native	I-Language
Interface	I-Language
(	O
JNI	B-Language
)	O
is	O
a	O
foreign	B-Application
function	I-Application
interface	I-Application
programming	O
framework	B-Architecture
that	O
enables	O
Java	B-Language
code	I-Language
running	O
in	O
a	O
Java	B-Language
virtual	I-Language
machine	I-Language
(	O
JVM	B-Language
)	O
to	O
call	O
and	O
be	O
called	O
by	O
native	O
applications	O
(	O
programs	O
specific	O
to	O
a	O
hardware	O
and	O
operating	B-General_Concept
system	I-General_Concept
platform	O
)	O
and	O
libraries	B-Library
written	O
in	O
other	O
languages	O
such	O
as	O
C	B-Language
,	O
C++	B-Language
and	O
assembly	B-Language
.	O
</s>
<s>
JNI	B-Language
enables	O
programmers	O
to	O
write	O
native	B-Language
methods	I-Language
to	O
handle	O
situations	O
when	O
an	O
application	O
cannot	O
be	O
written	O
entirely	O
in	O
the	O
Java	B-Language
programming	I-Language
language	I-Language
,	O
e.g.	O
</s>
<s>
when	O
the	O
standard	O
Java	B-Language
class	O
library	B-Library
does	O
not	O
support	O
the	O
platform-specific	O
features	O
or	O
program	B-Library
library	I-Library
.	O
</s>
<s>
It	O
is	O
also	O
used	O
to	O
modify	O
an	O
existing	O
application	O
(	O
written	O
in	O
another	O
programming	O
language	O
)	O
to	O
be	O
accessible	O
to	O
Java	B-Language
applications	O
.	O
</s>
<s>
Many	O
of	O
the	O
standard	O
library	B-Library
classes	O
depend	O
on	O
JNI	B-Language
to	O
provide	O
functionality	O
to	O
the	O
developer	O
and	O
the	O
user	O
,	O
e.g.	O
</s>
<s>
Including	O
performance	O
-	O
and	O
platform-sensitive	O
API	O
implementations	O
in	O
the	O
standard	O
library	B-Library
allows	O
all	O
Java	B-Language
applications	O
to	O
access	O
this	O
functionality	O
in	O
a	O
safe	O
and	O
platform-independent	O
manner	O
.	O
</s>
<s>
The	O
JNI	B-Language
framework	B-Architecture
lets	O
a	O
native	B-Language
method	I-Language
use	O
Java	B-Language
objects	O
in	O
the	O
same	O
way	O
that	O
Java	B-Language
code	I-Language
uses	O
these	O
objects	O
.	O
</s>
<s>
A	O
native	B-Language
method	I-Language
can	O
create	O
Java	B-Language
objects	O
and	O
then	O
inspect	O
and	O
use	O
these	O
objects	O
to	O
perform	O
its	O
tasks	O
.	O
</s>
<s>
A	O
native	B-Language
method	I-Language
can	O
also	O
inspect	O
and	O
use	O
objects	O
created	O
by	O
Java	B-Language
application	O
code	O
.	O
</s>
<s>
Only	O
applications	O
and	O
signed	O
applets	O
can	O
invoke	O
JNI	B-Language
.	O
</s>
<s>
An	O
application	O
that	O
relies	O
on	O
JNI	B-Language
loses	O
the	O
platform	O
portability	O
Java	B-Language
offers	O
(	O
a	O
partial	O
workaround	O
is	O
to	O
write	O
a	O
separate	O
implementation	O
of	O
JNI	B-Language
code	O
for	O
each	O
platform	O
and	O
have	O
Java	B-Language
detect	O
the	O
operating	B-General_Concept
system	I-General_Concept
and	O
load	O
the	O
correct	O
one	O
at	O
runtime	O
)	O
.	O
</s>
<s>
Not	O
only	O
can	O
native	O
code	O
interface	O
with	O
Java	B-Language
,	O
it	O
can	O
also	O
draw	O
on	O
a	O
Java	B-Language
,	O
which	O
is	O
possible	O
with	O
the	O
Java	B-Library
AWT	I-Library
Native	I-Library
Interface	I-Library
.	O
</s>
<s>
The	O
Java	B-Library
AWT	I-Library
Native	I-Library
Interface	I-Library
is	O
only	O
available	O
since	O
J2SE	B-Language
1.3	O
.	O
</s>
<s>
JNI	B-Language
also	O
allows	O
direct	O
access	O
to	O
assembly	B-Language
code	I-Language
,	O
without	O
even	O
going	O
through	O
a	O
C	B-Language
bridge	O
.	O
</s>
<s>
Accessing	O
Java	B-Language
applications	O
from	O
assembly	B-Language
is	O
possible	O
in	O
the	O
same	O
way	O
.	O
</s>
<s>
In	O
the	O
JNI	B-Language
framework	B-Architecture
,	O
native	O
functions	O
are	O
implemented	O
in	O
separate	O
.c	B-Language
or	O
.cpp	O
files	O
.	O
</s>
<s>
(	O
C++	B-Language
provides	O
a	O
slightly	O
simpler	O
interface	O
with	O
JNI	B-Language
.	O
)	O
</s>
<s>
When	O
the	O
JVM	B-Language
invokes	O
the	O
function	O
,	O
it	O
passes	O
a	O
JNIEnv	O
pointer	O
,	O
a	O
jobject	O
pointer	O
,	O
and	O
any	O
Java	B-Language
arguments	O
declared	O
by	O
the	O
Java	B-Language
method	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
following	O
converts	O
a	O
Java	B-Language
string	O
to	O
a	O
native	O
string	O
:	O
</s>
<s>
The	O
env	O
pointer	O
is	O
a	O
structure	O
that	O
contains	O
the	O
interface	O
to	O
the	O
JVM	B-Language
.	O
</s>
<s>
It	O
includes	O
all	O
of	O
the	O
functions	O
necessary	O
to	O
interact	O
with	O
the	O
JVM	B-Language
and	O
to	O
work	O
with	O
Java	B-Language
objects	O
.	O
</s>
<s>
Example	O
JNI	B-Language
functions	O
are	O
converting	O
native	O
arrays	B-Data_Structure
to/from	O
Java	B-Language
arrays	B-Data_Structure
,	O
converting	O
native	O
strings	O
to/from	O
Java	B-Language
strings	O
,	O
instantiating	O
objects	O
,	O
throwing	O
exceptions	O
,	O
etc	O
.	O
</s>
<s>
Basically	O
,	O
anything	O
that	O
Java	B-Language
code	I-Language
can	O
do	O
can	O
be	O
done	O
using	O
JNIEnv	O
,	O
albeit	O
with	O
considerably	O
less	O
ease	O
.	O
</s>
<s>
The	O
argument	O
obj	O
is	O
a	O
reference	O
to	O
the	O
Java	B-Language
object	O
inside	O
which	O
this	O
native	B-Language
method	I-Language
has	O
been	O
declared	O
.	O
</s>
<s>
Native	O
data	O
types	O
can	O
be	O
mapped	O
to/from	O
Java	B-Language
data	O
types	O
.	O
</s>
<s>
For	O
compound	O
types	O
such	O
as	O
objects	O
,	O
arrays	B-Data_Structure
and	O
strings	O
the	O
native	O
code	O
must	O
explicitly	O
convert	O
the	O
data	O
by	O
calling	O
methods	O
in	O
the	O
JNIEnv	O
.	O
</s>
<s>
A	O
JNI	B-Language
environment	O
pointer	O
(	O
)	O
is	O
passed	O
as	O
an	O
argument	O
for	O
each	O
native	O
function	O
mapped	O
to	O
a	O
Java	B-Language
method	O
,	O
allowing	O
for	O
interaction	O
with	O
the	O
JNI	B-Language
environment	O
within	O
the	O
native	B-Language
method	I-Language
.	O
</s>
<s>
This	O
JNI	B-Language
interface	O
pointer	O
can	O
be	O
stored	O
,	O
but	O
remains	O
valid	O
only	O
in	O
the	O
current	O
thread	O
.	O
</s>
<s>
Other	O
threads	O
must	O
first	O
call	O
to	O
attach	O
themselves	O
to	O
the	O
VM	O
and	O
obtain	O
a	O
JNI	B-Language
interface	O
pointer	O
.	O
</s>
<s>
Once	O
attached	O
,	O
a	O
native	O
thread	O
works	O
like	O
a	O
regular	O
Java	B-Language
thread	O
running	O
within	O
a	O
native	B-Language
method	I-Language
.	O
</s>
<s>
The	O
JNI	B-Language
framework	B-Architecture
does	O
not	O
provide	O
any	O
automatic	O
garbage	O
collection	O
for	O
non-JVM	O
memory	O
resources	O
allocated	O
by	O
code	O
executing	O
on	O
the	O
native	O
side	O
.	O
</s>
<s>
Consequently	O
,	O
native	O
side	O
code	O
(	O
such	O
as	O
assembly	B-Language
language	I-Language
)	O
assumes	O
the	O
responsibility	O
for	O
explicitly	O
releasing	O
any	O
such	O
memory	O
resources	O
that	O
the	O
native	O
code	O
acquires	O
.	O
</s>
<s>
On	O
Linux	O
and	O
Solaris	O
platforms	O
,	O
if	O
the	O
native	O
code	O
registers	O
itself	O
as	O
a	O
signal	O
handler	O
,	O
it	O
could	O
intercept	O
signals	O
intended	O
for	O
the	O
JVM	B-Language
.	O
</s>
<s>
A	O
chain	O
of	O
responsibility	O
can	O
be	O
used	O
to	O
allow	O
native	O
code	O
to	O
better	O
inter-operate	O
with	O
the	O
JVM	B-Language
.	O
</s>
<s>
On	O
Windows	O
platforms	O
,	O
Structured	O
Exception	O
Handling	O
(	O
SEH	O
)	O
may	O
be	O
employed	O
to	O
wrap	O
native	O
code	O
in	O
SEH	O
try/catch	O
blocks	O
so	O
as	O
to	O
capture	O
machine	O
(	O
CPU/FPU	O
)	O
generated	O
software	O
interrupts	O
(	O
such	O
as	O
NULL	O
pointer	O
access	O
violations	O
and	O
divide-by-zero	O
operations	O
)	O
,	O
and	O
to	O
handle	O
these	O
situations	O
before	O
the	O
interrupt	O
is	O
propagated	O
back	O
up	O
into	O
the	O
JVM	B-Language
(	O
i.e.	O
</s>
<s>
Java	B-Language
side	O
code	O
)	O
,	O
in	O
all	O
likelihood	O
resulting	O
in	O
an	O
unhandled	O
exception	O
.	O
</s>
<s>
The	O
following	O
table	O
shows	O
the	O
mapping	O
of	O
types	O
between	O
Java	B-Language
(	O
JNI	B-Language
)	O
and	O
native	O
code	O
.	O
</s>
<s>
In	O
addition	O
,	O
the	O
signature	O
"	O
L	O
qualified-class	O
;	O
"	O
would	O
mean	O
the	O
class	O
uniquely	O
specified	O
by	O
that	O
name	O
;	O
e.g.	O
,	O
the	O
signature	O
"	O
Ljava/lang/String	O
;	O
"	O
refers	O
to	O
the	O
class	O
java.lang.String	O
.	O
</s>
<s>
However	O
,	O
mapping	O
between	O
Java	B-Language
Strings	O
and	O
arrays	B-Data_Structure
to	O
native	O
strings	O
and	O
arrays	B-Data_Structure
is	O
different	O
.	O
</s>
<s>
If	O
a	O
jstring	O
is	O
used	O
where	O
a	O
char	O
*	O
would	O
be	O
,	O
the	O
code	O
could	O
crash	O
the	O
JVM	B-Language
.	O
</s>
<s>
JNI	B-Language
incurs	O
considerable	O
overhead	O
and	O
performance	O
loss	O
under	O
certain	O
circumstances	O
:	O
</s>
<s>
Function	O
calls	O
to	O
JNI	B-Language
methods	O
are	O
expensive	O
,	O
especially	O
when	O
calling	O
a	O
method	O
repeatedly	O
.	O
</s>
<s>
Native	B-Language
methods	I-Language
are	O
not	O
inlined	O
by	O
the	O
JVM	B-Language
,	O
nor	O
can	O
the	O
method	O
be	O
JIT	O
compiled	O
,	O
as	O
the	O
method	O
is	O
already	O
compiled	O
.	O
</s>
<s>
A	O
Java	B-Language
array	O
may	O
be	O
copied	O
for	O
access	O
in	O
native	O
code	O
,	O
and	O
later	O
copied	O
back	O
.	O
</s>
<s>
If	O
the	O
method	O
is	O
passed	O
an	O
object	O
,	O
or	O
needs	O
to	O
make	O
a	O
callback	O
,	O
then	O
the	O
native	B-Language
method	I-Language
will	O
likely	O
be	O
making	O
its	O
own	O
calls	O
to	O
the	O
JVM	B-Language
.	O
</s>
<s>
Accessing	O
Java	B-Language
fields	O
,	O
methods	O
and	O
types	O
from	O
the	O
native	O
code	O
requires	O
something	O
similar	O
to	O
reflection	B-Language
.	O
</s>
<s>
Signatures	O
are	O
specified	O
in	O
strings	O
and	O
queried	O
from	O
the	O
JVM	B-Language
.	O
</s>
<s>
Java	B-Language
Strings	O
are	O
objects	O
,	O
have	O
length	O
and	O
are	O
encoded	O
.	O
</s>
<s>
Microsoft	O
's	O
proprietary	O
implementation	O
of	O
a	O
Java	B-Language
Virtual	I-Language
Machine	I-Language
(	O
Visual	B-Application
J++	I-Application
)	O
had	O
a	O
similar	O
mechanism	O
for	O
calling	O
native	O
code	O
from	O
Java	B-Language
,	O
called	O
the	O
Raw	O
Native	O
Interface	O
(	O
RNI	O
)	O
.	O
</s>
<s>
In	O
addition	O
,	O
it	O
had	O
an	O
easy	O
way	O
to	O
call	O
existing	O
native	O
code	O
that	O
was	O
not	O
itself	O
aware	O
of	O
Java	B-Language
,	O
such	O
as	O
(	O
but	O
not	O
limited	O
to	O
)	O
the	O
Windows	O
API	O
,	O
called	O
J/Direct	O
.	O
</s>
<s>
However	O
,	O
following	O
the	O
Sun	O
–	O
Microsoft	O
litigation	O
about	O
this	O
implementation	O
,	O
Visual	B-Application
J++	I-Application
is	O
no	O
longer	O
maintained	O
.	O
</s>
<s>
RNI	O
was	O
less	O
clumsy	O
to	O
use	O
than	O
JNI	B-Language
,	O
because	O
no	O
bookkeeping	O
with	O
a	O
Java	B-Language
environment	O
pointer	O
was	O
needed	O
.	O
</s>
<s>
Instead	O
,	O
all	O
Java	B-Language
objects	O
could	O
be	O
accessed	O
directly	O
.	O
</s>
<s>
To	O
facilitate	O
this	O
,	O
a	O
tool	O
was	O
used	O
that	O
generated	O
header	O
files	O
from	O
Java	B-Language
classes	O
.	O
</s>
<s>
Similarly	O
,	O
J/Direct	O
was	O
easier	O
to	O
use	O
than	O
using	O
the	O
necessary	O
intermediate	O
native	B-Library
library	I-Library
and	O
JNI	B-Language
.	O
</s>
<s>
Java	B-Language
Native	I-Language
Access	I-Language
(	O
JNA	O
)	O
is	O
a	O
community-developed	O
library	B-Library
that	O
provides	O
Java	B-Language
programers	O
easy	O
access	O
to	O
native	O
shared	O
libraries	B-Library
without	O
using	O
JNI	B-Language
.	O
</s>
<s>
However	O
,	O
this	O
requires	O
the	O
redistribution	O
of	O
the	O
dependent	O
jar	O
library	B-Library
.	O
</s>
<s>
The	O
tradeoff	O
is	O
between	O
JNI	B-Language
being	O
harder	O
to	O
code	O
and	O
JNA	O
being	O
slower	O
.	O
</s>
<s>
JNI	B-Language
is	O
built	O
in	O
to	O
core	O
Java	B-Language
.	O
</s>
