Add the system call API in kvm in the j2-kvm tutorial

Source: Internet
Author: User

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:

 
 
  1. packageorg.test;  
  2. publicclassMiniTest  
  3. {  
  4. 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:

 
 
  1. externvoidJava_org_test_MiniTest_TestInt(void);  
  2.  

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:

 
 
  1. voidJava_org_test_MiniTest_TestInt(void)  
  2. {  
  3. 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:

 
 
  1. importorg.test.*;  
  2. classTest{  
  3. publicstaticvoidmain(String[]args){  
  4. System.out.println("TestResult="+MiniTest.TestInt());  
  5. }  

When compiling with javac, copy the previous j2me_cldc/classes.zip file and execute:

 
 
  1. javac-classpathclasses.zipTest.java  
  2.  
  3. kvm-classpath.Test  


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.