您知道Java Applet吗?它对Java的流行功不可没。
Applet一般运行于浏览器,那浏览器是怎么运行Java代码的呢?这就涉及到了今天要谈的主题。首先,浏览器会在本地构建一个Java VM实例,然后使用该实例来运行Java代码(Applet)。
这可以使用JNI中的The Invocation API方便的完成,要了解详细的示例您可以查看这里。
The Invocation API包括如下的接口:
有了这些接口我们就可以在本地代码中创建Java虚拟机来运行Java代码了。结合上一篇小文,我们就初步说明了JNI的双路调用。有了这样的基础,我们就可以发挥想象力来灵活使用JNI了。
在Java VM和Host Environment之间实现双路调用选择JNI(Java本地接口)是比较自然的,当然这也不是唯一的选择。
我们的选择也是JNI。它允许我们做两件事情,即所谓的双路调用:
其中,本地代码和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个步骤,照抄如下:
HelloWorld.java) that declares the native method.
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.
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.
HelloWorld.c) of the native method.
Hello-World.dll or libHello-World.so. Use the C compiler and linker available on the host environment.
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三家,各自的实现方式如下:
这种纷争非常不利于Java的发展,结果Sun出手了,从此也就有了现在的Java Native Interface(JNI),即Java本地接口。
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.
这种关系在现在还是很普遍的。
编程环境可以用来改善和提高程序员的生存质量,但是程序员们要保持警惕了,千万不要把自己置于某种定势之中。不过,现在有好多程序员都成了某种编程环境的“奴隶”,这是很遗憾的。

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.
不在沉默中爆发,就在沉默中灭亡 ... ...