Chapter 11: Java Native Interface

image from book Download CD Content

Introduction

The ability to communicate with native code such as Assembly, C, or C++ is fundamental for the operation and extensibility of nonnative languages such as Java. Applications that need to utilize OS or hardware capabilities need to do so in the appropriate language. Java code is compiled to bytecode, which is understood by the VM and not necessarily by the OS or the hardware. By allowing a program to execute some native code, the VM can avoid having to know about all the features of the underlying OS and hardware. This design allows for creation of packages that in turn use native code to utilize OS and hardware features. For example, when we create a new file in Java we use a package that in turn uses some native code to communicate with the OS. Because Java comes with extensive libraries, you can write entire applications without having to write any native code or even call native functions directly. Yet, there are times when you need to perform tasks not supported by the standard packages. In addition, a package may support certain operations but you might need to do them differently for reasons such as performance purposes. In addition, you might want to access existing native libraries such as OpenGL, DirectX, OpenAL, or even ones that you have created yourself.

Java Native Interface (JNI) allows you to mix native and Java code. If you want to make a professional Java game, it is vital to have at least one person on your team who is familiar with JNI. As it stands currently, you are likely to have to deal with native code directly. Even if you do not, it will be beneficial to be aware of your options during the development of your project. To understand this chapter, you should be familiar with C or C++. This chapter will teach you how to communicate with native code and vice versa. We will review the functionalities, provide examples, discuss the implications of using native code, and suggest usage patterns. For those already familiar with JNI, we recommend that you read sections that discuss direct byte buffers, which were introduced in JDK 1.4 as part of the java.nio package. Direct buffers have made it possible to share data between Java and native code in a direct and efficient manner. Games are one of the biggest beneficiaries of direct byte buffers.



Practical Java Game Programming
Practical Java Game Programming (Charles River Media Game Development)
ISBN: 1584503262
EAN: 2147483647
Year: 2003
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net