JavaScript must be enabled in order for you to view this page. However, it seems JavaScript is either disabled or not supported by your browser. To view this page, enable JavaScript by changing your browser options, then Try again! .

 
我的论坛
Google 网上论坛 Beta 版
Do Best Developer
访问此论坛

如何从本地代码调用Java代码?

您知道Java Applet吗?它对Java的流行功不可没。

Applet一般运行于浏览器,那浏览器是怎么运行Java代码的呢?这就涉及到了今天要谈的主题。首先,浏览器会在本地构建一个Java VM实例,然后使用该实例来运行Java代码(Applet)。

这可以使用JNI中的The Invocation API方便的完成,要了解详细的示例您可以查看这里

The Invocation API包括如下的接口:

  • JNI_GetDefaultJavaVMInitArgs 返回Java虚拟机的默认初始化参数
  • JNI_GetCreatedJavaVMs 返回所有已经创建的Java虚拟机
  • JNI_CreateJavaVM 加载和初始化一个Java虚拟机
  • DestroyJavaVM 卸载Java虚拟机和释放资源
  • AttachCurrentThread 将当前线程配给到Java虚拟机(Attaches the current thread to a Java VM)
  • DetachCurrentThread 从Java虚拟机中分离当前线程(Detaches the current thread from a Java VM)

有了这些接口我们就可以在本地代码中创建Java虚拟机来运行Java代码了。结合上一篇小文,我们就初步说明了JNI的双路调用。有了这样的基础,我们就可以发挥想象力来灵活使用JNI了。

芝麻开门

在Java VM和Host Environment之间实现双路调用选择JNI(Java本地接口)是比较自然的,当然这也不是唯一的选择。

我们的选择也是JNI。它允许我们做两件事情,即所谓的双路调用:

  • 从Java代码调用本地代码
  • 从本地代码调用Java代码

其中,本地代码和Host Environment密切相关,通常为用c或c++书写的代码。

今天我们通过Hello World示例来感受一下从Java代码调用本地代码的大致过程。这一过程在The Java Native Interface Programmer's Guide and Specification中有详尽的描述,看这里 Chapter 2 Getting Started

值得强调的是在Chapter 2 Getting Started开始提到的6个步骤,照抄如下:

  1. Create a class (HelloWorld.java) that declares the native method.
  2. Use javac to compile the HelloWorld source file, resulting in the class file HelloWorld.class. The javac compiler is supplied with JDK or Java 2 SDK releases.
  3. Use javah -jni to generate a C header file (HelloWorld.h) containing the function prototype for the native method implementation. The javah tool is provided with JDK or Java 2 SDK releases.
  4. Write the C implementation (HelloWorld.c) of the native method.
  5. Compile the C implementation into a native library, creating Hello-World.dll or libHello-World.so. Use the C compiler and linker available on the host environment.
  6. Run the HelloWorld program using the java runtime interpreter. Both the class file (HelloWorld.class) and the native library (HelloWorld.dll or libHelloWorld.so) are loaded at runtime.

明天我们讨论 从本地代码调用Java代码

起于乱世

Sun好象很愿意成为乱世的终结者。看,这次它又出手了。

在Java的早期,如何在JVM和Host Environment之间实现双路调用还没有定论,这就给各大厂商留出了争霸的空间。

比较有名的当属Sun、Netscape和Microsoft三家,各自的实现方式如下:

  • Sun -> JDK 1.0 Native Method Interface
  • Netscape -> Java Runtime Interface
  • Microsoft -> Raw Native Interface and Java/COM Interface

这种纷争非常不利于Java的发展,结果Sun出手了,从此也就有了现在的Java Native Interface(JNI),即Java本地接口。

Programmer & Program Environment

The typical relationship between the programmer and the programming environment is one where the environment nurtures the programmer. In return for this support,the programmer works within the constraints created by the environment.

From Contributing to Eclipse.

这种关系在现在还是很普遍的。

编程环境可以用来改善和提高程序员的生存质量,但是程序员们要保持警惕了,千万不要把自己置于某种定势之中。不过,现在有好多程序员都成了某种编程环境的“奴隶”,这是很遗憾的。

The Plug-in Architecture of Eclipse

plug-in

We should have a little bit of backgroud about the plug-in acchitecture of eclipse.

Eclipse is a collection of places-to-plug-things-in (extension points) and things-plugged-in (extensions). The powerstrip is a kind of extension point. Multiple extensions (in this case,power plugs) can plug into it,and although the extensions are different shapes and have different purposes,they all must share a common interface.

From Contributing to Eclipse.

超越自己

浑浑噩噩的长假结束了,浑浑噩噩的自己还没有解脱 ... ...

任何人都不能通过感染来结束这样的浑浑噩噩,任何人都不能。那自己可以吗?也许吧!不过要比高喊“超越自己”要难的多。

... ...

不在沉默中爆发,就在沉默中灭亡 ... ...