Page 159 - The Definitive Guide to Building Java Robots
P. 159
Preston_5564C05.fm Page 140 Tuesday, September 20, 2005 5:13 AM
140 CHAPTER 5 ■ SPEECH
The Java Native Interface (JNI)
JNI allows programs that run within the Java Virtual Machine (JVM) to operate with applications
and libraries written in other languages. To illustrate, let’s create a simple native code example
using Visual C++.
1. Open Visual Studio.
2. Select Managed C++ Library. If it’s named SimpleJNI, the tool will create the following files:
• AssemblyInfo.cpp: C++ source; contains custom attributes
• SimpleJNI.cpp: C++ source; the main file in .dll source
• SimpleJNI.h: Header file
• SimpleJNI.ncb: IntelliSense database
• SimpleJNI.sln: Solution file
• SimpleJNI.suo: Solution Options file
• SimpleJNI.vcproj: Project file
• ReadMe.txt: ReadMe file
• Stdafx.cpp: C++ source; contains standard system includes
• Stdafx.h: C++ header; contains standard system includes
3. Create your native Java class. In Example 5-1, I’ll call this TempConvert.
Example 5-1. TempConvert.java
package com.scottpreston.javarobot.chapter5;
public class TempConvert {
// this is a DLL in system path SimpleJNI.dll
static {
System.loadLibrary("SimpleJNI");
}
// native method
public native float CtoF(float c);
// native method
public native float FtoC(float f);