⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java如何调用c-c++(jni).mht

📁 JAVA核心源代码及算法,适合入门的初学者,可以让你快的入门
💻 MHT
📖 第 1 页 / 共 5 页
字号:
            =
value=3Dhttp://www.cn-java.com/webteam/>|-JWT=B3=C9=D4=B1</OPTION></SELEC=
T>=20
            </FONT></FONT></TD></TR></TBODY></TABLE></TD></TR>
  <TR>
    <TD>
      <TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"98%" =
align=3Dcenter=20
      bgColor=3D#ffffff border=3D0>
        <TBODY>
        <TR>
          <TD vAlign=3Dtop align=3Dmiddle height=3D69>
            <TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%" =
bgColor=3D#ffffff=20
            border=3D0>
              <TBODY>
              <TR>
                <TD vAlign=3Dtop height=3D69>
                  <TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"95%" =
align=3Dcenter=20
                  border=3D0>
                    <TBODY>
                    <TR>
                      <TD width=3D"99%">
                      <TD width=3D"1%">
                    <TR>
                      <TD width=3D"99%"><BR>
                        <P><FONT face=3D"=CB=CE=CC=E5,Arial, Helvetica, =
sans-serif"=20
                        color=3D#000000 size=3D2></FONT><FONT=20
                        face=3D"=BA=DA=CC=E5,Arial, Helvetica, =
sans-serif" color=3D#000000=20
                        size=3D4>Java=C8=E7=BA=CE=B5=F7=D3=C3C/C++(JNI) =
</FONT><FONT color=3Dgray=20
                        size=3D1>=D4=C4=B6=C1=B4=CE=CA=FD1727</FONT>=20
                        <P align=3Dright>=B3=F6=B4=A6 =
=BB=E1=D4=B1=A3=BAsinal(jdeveloper=20
                        =
/java=BF=AA=B7=A2=D5=DF)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</P>
                        <P></P>
                        <HR noShade>

                        <TABLE cellSpacing=3D0 cellPadding=3D0 =
width=3D"100%"=20
border=3D0>
                          <TBODY>
                          <TR>
                            <TD vAlign=3Dtop =
width=3D"86%">Java=C8=E7=BA=CE=B5=F7=D3=C3C/C++(JNI)=20
                              =D7=F7=D5=DF=A3=BAjdeveloper <BR><BR>1. =
program Java source file=20
                              loading Native method<BR>
                              <TABLE cellSpacing=3D0 cellPadding=3D5 =
width=3D"100%"=20
                              bgColor=3D#cccccc border=3D1><BR><BR><BR>
                                <TBODY>
                                <TR>
                                <TD><PRE><CODE><BR>
//<BR>
//<BR>
// NativeDemo<BR>
// Author: huang_jc <BR>
// file name: NativeDemo.java<BR>
public class NativeDemo <BR>
{  <BR>
         int i;<BR>
    int j;<BR>
    public static void main(String args[])<BR>
    {<BR>
             NativeDemo ob =3D new NativeDemo();<BR>
           ob.i =3D 10;<BR>
            ob.j =3D ob.test();<BR>
            System.out.println("this is ob.i:"+ob.i+"\n");<BR>
            System.out.println("this is ob.j:"+ob.j);<BR>
    } <BR>
    public native int test();<BR>
    static <BR>
    {<BR>
     System.loadLibrary("NativeDemo");    <BR>
    }<BR>
}<BR>
</CODE></PRE></TD></TR></TBODY></TABLE><BR><BR>2.Compile=20
                              file NativeDemo.java <BR>Javac=20
                              NativeDemo.java<BR><BR>3.Use javah.exe to =
produce=20
                              file NativeDemo.c and NativeDemo.h<BR>a : =
javah=20
                              NativeDemo to produce NativeDemo.h which =
is:<BR>
                              <TABLE cellSpacing=3D0 cellPadding=3D5 =
width=3D"100%"=20
                              bgColor=3D#cccccc border=3D1><BR><BR><BR>
                                <TBODY>
                                <TR>
                                <TD><PRE><CODE><BR>
/* DO NOT EDIT THIS FILE - it is machine generated */<BR>
#include <NATIVE.H><BR>
/* Header for class NativeDemo */<BR>
<BR>
#ifndef _Included_NativeDemo<BR>
#define _Included_NativeDemo<BR>
<BR>
#pragma pack(4)<BR>
<BR>
typedef struct ClassNativeDemo {<BR>
long i;<BR>
long j;<BR>
} ClassNativeDemo;<BR>
HandleTo(NativeDemo);<BR>
<BR>
#pragma pack()<BR>
<BR>
#ifdef __cplusplus<BR>
extern "C" {<BR>
#endif<BR>
extern long NativeDemo_test(struct HNativeDemo *);<BR>
#ifdef __cplusplus<BR>
}<BR>
#endif<BR>
#endif<BR>
b : use javah -stubs NativeDemo to produce NativeDemo.c which seems =
as:<BR>
/* DO NOT EDIT THIS FILE - it is machine generated */<BR>
#include <STUBPREAMBLE.H><BR>
<BR>
/* Stubs for class NativeDemo */<BR>
/* SYMBOL: "NativeDemo/test()I", Java_NativeDemo_test_stub */<BR>
__declspec(dllexport) stack_item *Java_NativeDemo_test_stub(stack_item =
*_P_,struct execenv *_EE_) {<BR>
    extern long NativeDemo_test(void *);<BR>
    _P_[0].i =3D NativeDemo_test(_P_[0].p);<BR>
    return _P_ + 1;<BR>
}</CODE></PRE></TD></TR></TBODY></TABLE><BR>4.=20
                              Write your own implimentation of method=20
                              test()<BR>//file name test.c<BR>
                              <TABLE cellSpacing=3D0 cellPadding=3D5 =
width=3D"100%"=20
                              bgColor=3D#cccccc border=3D1><BR><BR><BR>
                                <TBODY>
                                <TR>
                                <TD><PRE><CODE><BR>
#include <STUBPREAMBLE.H><BR>
#include "nativedemo.h"<BR>
#include <STDIO.H><BR>
<BR>
long NativeDemo_test(HNativeDemo *this)<BR>
{<BR>
    printf("hello:!\n");<BR>
    printf("this is in the native method!\n");<BR>
    return (unhand(this)-&gt;i)*2;<BR>
}<BR>
</CODE></PRE></TD></TR></TBODY></TABLE><BR>5. link=20
                              NativeDemo.c and test.c to produce=20
                              NativeDemo.dll<BR>Use VC++5.0 tools =
:Cl.exe <BR>Cl=20
                              /GD NativeDemo.c test.c /LD<BR>then we get =

                              =
NativeDemo.dll<BR><BR>=CF=B5=CD=B3=BD=AB=D3=D0=C8=E7=CF=C2=CC=E1=CA=BE=A3=
=BA<BR><BR>NativeDemo.c<BR>test.c<BR>Generating=20
                              =
Code...<BR><BR>/out:NativeDemo.dll<BR>/dll<BR>/implib:NativeDemo.lib<BR>N=
ativeDemo.obj<BR>test.obj<BR>Creating=20
                              library NativeDemo.lib and object=20
                              NativeDemo.ex<BR>Press any key to continue =

                              <BR><BR><BR><BR><BR>6.Run <BR><BR>java=20
                              NativeDemo<BR><BR>we get the such=20
                              result:<BR><BR>D:\vj11user\native&gt;java=20
                              NativeDemo<BR>hello:!<BR>this is in the =
native=20
                              method!<BR>this is ob.i:10<BR><BR>this is=20
                              ob.j:20<BR><BR>D:\vj11user\native&gt;=20
                              <BR><BR><BR>NOTE:<BR>Do not forget this:=20
                              <BR>c:&gt; set=20
                              =
INCLUDE=3Dd:\java\include;d:\java\win32;%INCLUDE%<BR>C:&gt;set=20
                              LIB =3Dd:\java\lib;%LIB%<BR>where d:\java =
is the=20
                              directory of your jdk<BR><BR>new version =
jdk1.2.1=20
                              :<BR><BR>With new jdk1.2.1 =
:<BR><BR>1.javac=20
                              NativeDemo.java<BR><BR>2. javah NativeDemo =
to=20
                              produce head file (You need not to javah =
-stubs=20
                              NativeDemo to produce NativeDemo.c=20
                              file),<BR><BR>You don't need the c file. =
Just the=20
                              NativeDemo.h is sufficient!<BR><BR>3.Use =
VC++=20
                              produce a window dll project and add the=20
                              NativeDemo.h to it.<BR><BR>then implement =
the=20
                              function. All is OK!<BR>NOTE: the data =
type=20
                              conversion between java and c++.See jni.h =
for more=20
                              information.<BR><BR></TD>
                            <TD vAlign=3Dtop width=3D"14%">
                              <DIV align=3Dright><FONT=20
                              =
color=3Dgray>&nbsp;&nbsp;&nbsp;&nbsp;</FONT></DIV>
                              <SCRIPT type=3Dtext/javascript><!--
google_ad_client =3D "pub-1552958637257019";
google_ad_width =3D 160;
google_ad_height =3D 600;
google_ad_format =3D "160x600_as";
google_ad_channel =3D"";
google_ad_type =3D "text";
google_color_border =3D "A8DDA0";
google_color_bg =3D "EBFFED";
google_color_link =3D "0000CC";
google_color_url =3D "008000";
google_color_text =3D "6F6F6F";
//--></SCRIPT>

                              <SCRIPT=20
                              =
src=3D"http://pagead2.googlesyndication.com/pagead/show_ads.js"=20
                              type=3Dtext/javascript>
</SCRIPT>
                            </TD></TR></TBODY></TABLE>
                        <P></P></TD>
                      <TD vAlign=3Dtop width=3D"1%">&nbsp; </TD></TR>
                    <TR>
                      <TD width=3D"99%"></TD>
                      <TD =
width=3D"1%">&nbsp;</TD></TR></TBODY></TABLE></TD>
              <TR>
                <TD vAlign=3Dcenter =
height=3D73></TD></TR></TBODY></TABLE></TD>
        <TR>
          <TD vAlign=3Dtop height=3D73>
            <TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"98%" =
align=3Dcenter=20
border=3D0>
              <TBODY>
              <TR>
                <TD width=3D"1%" height=3D31>&nbsp;</TD>
                <TD width=3D"99%" height=3D31>
                  <P align=3Dleft><FONT color=3Dred><IMG=20
                  =
src=3D"http://www.cn-java.com/images/smalllogo.gif">=D5=FB=C0=ED=B7=A2=B2=
=BC</FONT></P>
                  <P align=3Dright>&nbsp;</P>
                  <P align=3Dleft>&nbsp;</P>
                  <TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%" =
border=3D0>
                    <TBODY>
                    <TR>
                      <TD bgColor=3D#dcdce7>
                        <TABLE cellSpacing=3D0 cellPadding=3D0 =
width=3D"96%"=20
                        align=3Dcenter border=3D0>
                          <TBODY>
                          <TR>
                            <TD>&nbsp;<FONT=20
                              =
color=3Dred>=B7=A2=D1=D4=C8=CB:</FONT><B>gflei</B>&nbsp;&nbsp;&nbsp;=C8=D5=
=C6=DA=A3=BA2002-03-08
                              <HR>
                              =
=C8=EB=C3=C5=B2=C4=C1=CF=B2=BB=B4=ED=A3=AC=B2=BB=B9=FD=D3=D0=C3=BB=D3=D0=BA=
=F3=D0=F8=D7=F7=C6=B7=A3=BF=D2=BB=D0=A9=CA=B5=D6=CA=CE=CA=CC=E2=B5=C4=BD=E2=
=BE=F6=B7=BD=B0=B8=A3=BF<BR><BR><BR></TD></TR></TBODY></TABLE></TD></TR><=
/TBODY></TABLE>
                  <FORM name=3DformMsg =
action=3DsendPinglun.php?tg=3Dnews.php=20
                  method=3Dpost>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -