资源预览内容
第1页 / 共19页
第2页 / 共19页
第3页 / 共19页
第4页 / 共19页
第5页 / 共19页
第6页 / 共19页
第7页 / 共19页
第8页 / 共19页
第9页 / 共19页
第10页 / 共19页
亲,该文档总共19页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
使用非java代码(Use non java code)Use non JAVA codeJAVA language and its standard API (Application Programming Interface) are more than enough to write applications. But in some cases, the non JAVA code must be used. For example, we sometimes access specific features of operating systems, deal with special hardware devices, reuse existing non Java interfaces, or use time sensitive code segments, and so on. With non Java code communication requires special support from the compiler and the virtual machine, and the Java code is mapped to Java code additional tools (there is also a simple method: in the fifteenth chapter a Web section, there is an example to explain how to use the standard input and output with non Java code to connect). At present, different developers provide different solutions for us: Java 1.1 Java (Java Native Interface natural interface, JNI, Netscape) proposed the Java runtime interface in their own (Java Runtime Interface) program, Microsoft provides J/Direct, the source interface (Raw Native Interface, RNI) and Java/COM integration scheme.The different attitudes of developers on this issue are very bad for programmers. If the Java application must invoke the inherent method, the programmer may have to implement different versions of the inherent method - specifically determined by the platform that the application runs. Programmers may actually need different versions of Java code, as well as different Java virtual machines.Another solution is CORBA (Generic Object Request Broker Architecture), which is an integration technology developed by OMG (object management group, a non-profit association of companies). CORBA is not a part of any language, but a specification for universal communications buses and services. It can realize the ability of mutual operation between objects implemented by different languages. The name of the communication bus, called ORB (object request broker), is a product implemented by other developers, but not part of the Java language specification.Java inherent interfaceJNI is an extremely inclusive programming interface that allows us to invoke inherent methods from Java applications. It is added in Java 1.1, maintaining the corresponding characteristic of Java 1 - inherent method interface (NMI) - to some degree of compatibility. Some of the features of NMI design make it not supported by all virtual machines. For this reason, the future version of the Java language may no longer support NMI, nor is it ready to discuss it here.At present, JNI can only deal with the inherent methods written in C or C+. Using JNI, our inherent method can be:- create, check and update Java objects (including arrays and strings)- call the Java method- capture and discard abnormal- class loading and access to information- for run-time type checkingSo, almost everything that can be done to classes and objects in Java can be done in the same way.Call inherent methodStart with a simple example: a Java program calls the inherent method, and the latter calls Win32s API function MessageBox (), showing a graphical text box. This example will later use J/Direct and ichishi. If your platform is not Win32, just include the C header with the following contents:#include Replace:#include And the call to MessageBox () is replaced by calling printf ().The first step is to write the Java code that declares the intrinsic method and its independent variables:Behind the inherent in the method declaration, followed by a static code block, it will call (System.loadLibrary) (available at any time to call it, but it is more appropriate) System.loadLibrary (DLL) will be a loaded into memory, and establish links with it. DLL must be in your system path, or in the directory that contains the Java class file.According to the specific platform, JVM will automatically add appropriate file extensions.1. C header file generator: javahNow Java source file, and the compiled.Class file run javah. Javah is provided in version 1, but because Java 1.1 JNI must be used, the -jni parameter must be specified:Javah -jni ShowMsgBoxJavah reads the class file and generates a function prototype for each intrinsic method declared in the C or C+ header file.From the preprocessing command of #ifdef_cplusplus, you can see that the file can be compiled either by the C compiler or by the C+ compiler. The first jni.h - #include command includes a header file, is one of functions defined in the file the rest used type; JNIEXPORT and JNICALL are some macros, they are appropriate to expand, and those different platform specific guidance command with JNIEnv, jobject and jstring; JNI is a data type definition.2. name management and function signatureJNI unifies the naming rules of intrinsic methods; this is important because it is part of the mechanism that virtual machines link Java calls t
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号