Compiling and Running a Java Program with a Native Method
2010-03-08 11:43
501 查看
Thanks :http://www.iam.ubc.ca/guides/javatut99/native1.1/stepbystep/step1.html
Step 1: Write the Java Code
The following Javacode segment defines a class named
HelloWorld
.
This class declares one native method, implements a main method, and has a static code segment.
class HelloWorld { public native void displayHelloWorld(); static { System.loadLibrary("hello"); } public static void main(String[] args) { new HelloWorld().displayHelloWorld(); } }
Declare a Native Method
You must declare all methods, whether Java methods or native methods,within a class on the Java side. When you write a method implementation
in a language other than Java, you must include the keyword
native
as part of the method's definition within the Java class. The
native
keyword signals to the Java compiler that the function is a native
language function. It is easy to tell that the implementation for the
HelloWorld
class's
displayHelloWorld
method is written in another programming language
because the
native
keyword appears as part of its method
definition:
public native void displayHelloWorld();
This native method declaration in your Java class provides only the method signature for
displayHelloWorld
. It provides no implementation for the method. You must provide the implementation for
displayHelloWorld
in a separate native language source file.
The method declaration for
displayHelloWorld
also indicates that the method
is a public instance method, accepts no arguments, and returns no value. For more information
about arguments to and return values from native methods see
Interacting with Java from the Native Side
![](http://www.iam.ubc.ca/guides/javatut99/images/jdk1_1.gif)
.
Load the Library
You compile the native language code that implementsdisplayHelloWorld
into a
shared library (you will do this in Step 5: Create a Shared Library
).
The runtime system later loads the shared library into the Java class that requires it. Loading the
library into the Java class maps the implementation of the native method to its declaration.
The
HelloWorld
class uses the
System.loadLibrary
method. The
System.loadLibrary
method loads the shared library that will be created when you compile
the implementation code. Place this method within a static initializer.
The argument to
System.loadLibrary
is the shared library
name. This can be any name that you choose. The system uses a standard,
but platform-specific, approach to convert the library name to a native
library name. For example, the Solaris system converts the library name
"hello" to
libhello.so
, while a Win32 system converts the same name to
hello.dll
.
The following static initializer from the
HelloWorld
class loads the
appropriate library, named
hello
. The runtime system executes a class's static initializer when it loads the class.
static { System.loadLibrary("hello"); }
Write the Main Method
TheHelloWorld
class, because it is an application, also includes a
main
method to instantiate the class and call the native method. The
main
method instantiates
HelloWorld
and
calls the
displayHelloWorld
native method.
public static void main(String[] args) { new HelloWorld().displayHelloWorld(); }
You can see from the code sample that you call a native method in
the same manner as you call a regular method: just append the name
of the method to the end of the object name, separated with a period ('.'). A matched
set of parentheses, (), follow the method name and enclose any arguments
to pass into the method. The
displayHelloWorld
method doesn't
take any arguments.
相关文章推荐
- Java Method Logging with AOP and Annotations
- spring.data.jpa.query :Cannot use native queries with dynamic sorting and/or pagination in method
- SpringData JPA @Query分页查询 Cannot use native queries with dynamic sorting and/or pagination in method
- Understanding FastCGI and “Gotchas” with Running Native Code
- writing a java program with Native methods
- The ins and outs of using Java with Domino
- java.lang.UnsatisfiedLinkError: Native method not found: com.baidu.platform.comjni.engine.JNIEngine.
- When running gradle with java 5, 6 or 7, you must set the path to jdk8, either with property retrola
- How Synchronization works in Java ? Example of synchronized Block and Method
- How to use Comparator and Comparable in Java? With example
- BaiduMap提示 java.lang.UnsatisfiedLinkError: Native method not found: com.baidu.mapapi.Mj.initClass:(L
- Up and running with Apache Spark on Apache Kudu
- Application binary interface and method of interfacing binary application program to digital computer
- Simplifying Graphics With Java and Threads
- Developing with Comet and Java[转]
- Android JNI报错:java.lang.UnsatisfiedLinkError: Native method not found ..........解决办法
- How to Get Started With JMeter: Part 2 - Building Scripts and Running JMeter
- CQRS and Event Sourcing in Java with Spring Framework
- Android学习笔记-迁移报错command 'C:\Program Files\Java\jdk1.7.0_67\bin\java.exe'' finished with non-zero1
- Android使用百度地图SDK调用百度地图"java.lang.UnsatisfiedLinkError: Native method not found: com.baidu.platform."