Do you know how to add system calling APIs in KVM? Here we will describe different systems calling APIs and their implementations for different j2s devices, I believe this article will surely help you gain some benefits.
How to add the system call API in KVM
We all know that KVM does not support Native function calls. If you want to add some system-called APIs, you can only add them to KVM. At the same time, different j2's devices also have different systems that call APIs and their implementations. How can we add a system call API for the original KVM source code downloaded from SUN? This document describes how to add KVM system APIs step by step.
In fact, adding a system call API for KVM is easier than adding a system call API for linux. It is roughly divided into two steps to complete. One step is to add a new class in classes.zip. One step is to implement the nativeapi function of this new class in nativeCore. c of KVM.
The following uses the newly added class org. test. MiniTest as an example to implement a TestInt () System Call function. The function is simple, that is, return an integer of 9999.
1. added the org. test. MiniTest class.
Download the KVM code of j2me_cldc1.1 from SUN. Add the orgestMiniTest. Java package directory and java file under the j2me_cldcapi directory. Then write the following code:
- packageorg.test;
- publicclassMiniTest
- {
- publicstaticnativeintTestInt();
- }
2. Perform the first compilation
According to the KVM Compilation Method in the previous article, at the command prompt, jump to the j2me_cldcuildwin32 directory and enter the make command for the first overall compilation. However, when compiling the *. o file in KVM, an error message indicating _ Java_org_test_MiniTest_TestInt is displayed.
This is because the native function TestInt is defined in org. test. MiniTest, but the corresponding function is not implemented in any c file of KVM.
First, the compilation process is to use javac to compile all * in j2me_cldc/api to generate ROMJavaWin. c and nativeFunctionTableWin. c. In ROMJavaWin. c, declare the native function:
- externvoidJava_org_test_MiniTest_TestInt(void);
-
3. Implement the Java_org_test_MiniTest_TestInt Function
From the code in KVM, we can see that KVM puts some native functions into the nativeCore. c file by default. You can also add some C program files by yourself, but in this example, the Java_org_test_MiniTest_TestInt is placed in the nativeCore. c file.
The following code is used:
- voidJava_org_test_MiniTest_TestInt(void)
- {
- pushStack(9999);
- }
The reason why the return value is returned using the pushStack macro is hard to say. The JAVA running method is actually a stack, and the Java bytecode is actually a stack-based language, the prototype and description of the intermediate code generation chapter in the compilation principle can be found. You can also read the book InsideJavaVirtualMachine.
4. Second compilation
The second compilation attempt can generate a genuine kvm.exe file. Just as with the first compilation and the previous compilation of KVM, just press the make command.
5. Test the MiniTest. TestInt API.
Write a Test class to Test this API:
- importorg.test.*;
- classTest{
- publicstaticvoidmain(String[]args){
- System.out.println("TestResult="+MiniTest.TestInt());
- }
- }
When compiling with javac, copy the previous j2me_cldc/classes.zip file and execute:
- javac-classpathclasses.zipTest.java
-
- kvm-classpath.Test