Glossary
Before getting started with the introduction of J2EE, I will list some common terms for convenience:
J2EE(Java2 Platform, Micro Edition) Micro version of Java2 Platform
Cldc(Connected, limited device configuration) configure the connection to a restricted Device
CDC(Connected device configuration) Connection Device Configuration
MIDPMobile Information device Profile
KVM(The K Virtual Machine) K Virtual Machine
Background
With the increasing importance of wireless applications and embedded systems, Sun has also launched Java2 Platform (Micro Edition) to cater to this future development trend.
In fact, this is not the latest release. Anyone familiar with Java knows that Java was originally developed to develop consumer electronic products. Which of the following is why network applications have become increasingly popular, such as J2EE and EJB, because of their outstanding performance in network applications. Sun has launched another j2-based player, which actually kills a Trojan horse. We have to wait and see how it actually works.
Sun believes that in the future, information devices will be more custom and personal than we do today. We can see the popularity of mobile phones and PDAs. Therefore, to develop applications for these devices. Sun has released the j2-based architecture. As a result, the existing Java version has changed to three: J2EE, j2se, and j2s, which correspond to different target devices, as shown in.
Figure 1 Java version division and target devices
Basic Structure of cldc/MIDP
In a rough sense, devices are divided into two categories: Connection Device and limited device ). The former one corresponds to those devices with power supply, abundant power supply, and large power supply. For example: TV set, refrigerator, etc. The latter corresponds to the main battery and small-sized devices, such as mobile phones and PDAs.
Here I mainly only involve the second type, limited connection device (connected, limited device ).
Sun divides the implementation of J2EE into two layers: configuration and profile.
Configuration includes virtual machines, core class libraries, and APIs. The configuration layer defines the features of a Java Virtual Machine and the minimum subset of the Java class library. That is to say, the configuration layer provides a basic and core Java platform for developers. Because the devices are divided into two categories: the connected device and the limited device ). The two devices have two types of j2_configuration. They are CDC (connected device configuration) and cldc (connected, limited device configuration ). Note that CDC uses the classic JavaVM, while cldc uses KVM (the K virtual machine ). KVM is a 16/32-bit CPU or controller dedicated by Sun, with a memory of KB to kb ~ Developed for devices of kb. KVM is relatively small, usually only K or less.
The profile layer also contains a set of APIS, which are defined mainly for devices of a specific family. Profile layer in a specific
Implementation on the configuration layer, while weProgramDevelopers are responsible for writing applications on specific profiles. For mobile phones, PDAs, and other connected and limited devices. Their profile layer is called MIDP (mobile information device profile ). Therefore, the combination of MIDP and cldc forms a complete j2_architecture. By the way, sun, an application on MIDP, is also called a MIDlet. Do you think it is a bit eye-catching. Think about the applet in Java. Shows the structure of the mobile app.
Figure 2 Structure of cldc/MIDP
In-depth understanding of cldc/MIDP
Purpose of cldc
- A standard Java platform is defined for a small, connectedlimited device with limited resources.
- Allows dynamic publishing of content and applications to these devices.
- Third-party software vendors and developers can easily transplant content and applications to these devices.
Range of cldc
- Java language and features of virtual machines
- Core Java class libraries (such as Java. Lang. *, java. util .*)
- Input/Output
- Network Support
- Security Support
- International support
The range not included by cldc (that is, it is resolved by MIDP)
- Manage the application life-cycle (that is, how an application is installed, run, and deleted)
- User Interface)
- Event Processing
- Advanced Application Mode (the interaction between the user and the application ).
Security of cldc
The low-end virtual machine-based security is guaranteed by the Verification step of the class file during download. (If you have written and compiled the MIDlet program, you will know that this step is required. Here is a concept. I will describe how to create a MIDlet program in detail ). Application-based security ensures that each application runs in a relatively independent closed environment, and classes in the protected system package cannot be overwritten by the application ).
Special Aspects of Java support
Cldc is fully compatible with the Java language, but pay attention to the following points:
It does not support floating-point data types (that is, there is no float or double) (this is mainly because cldc is oriented to devices, and its hardware does not support floating-point operations due to its memory limitations or security considerations)
The finalization of a class instance is not supported, that is, the object. Finalize () method is not stored. (Note: finalization refers to another object cleanup method provided by Java and garbage collection. For details, refer to Bruce Eckel's thinking in Java version 2 translated by Hou Jie, which can be downloaded from www.jjhou.com)
Limited error handling, that is, most java. Lang. Error subclasses are not supported. In contrast, cldc includes a fairly complete exception class.
Special Aspects supported by Java Virtual Machine
In essence, cldc imposes a considerable degree of compatibility with Java virtual machines in its strict memory limits. However, there are the following differences:
- Float and double data types are not supported)
- JNI (the Java Native Interface) is not supported. It does not support custom Java-level class loaders)
- Reflection is a function provided by Java. Lang. reflect to analyze the capabilities of classes. It is often used to check the structure of classes and is widely used in JavaBeans. For details, see Core Java2 volumei, II of Gary Cornell. The mechanic has an out-of-Chinese translation)
- Thread groups or daemon threads are not supported)
- Finalization is not supported)
- No weak references (weak references) (Note: weak reference provides a method to point to objects that have been cleared by garbage collector. It is introduced in Java2. For more information, see the java. Lang. Ref API documentation andArticle.)
- Limited Error Handling)
Class file review process (classfiles verification)
Cldc requires its lower-level virtual machines to identify and reject illegal class files. However, cldc is designed for small devices with low memory consumption. The file-like detection mechanism is different from the standard file audit mechanism defined in j2se. Note:
Figure 3 cldc class audit mechanism
Note the preverification process in cldc. This is the key to distinguishing cldc from the common file-like review process. As shown in, after the source program is compiled, it must be pre-reviewed by the Pre-reviewer before it can be downloaded to the target device. This step is mainly used to reduce the burden on the KVM reviewer and speed up the audit. As I mentioned above, this is out of consideration for the hardware supported by cldc (after all, mobile phones and PDAs do not have the Pentium "core" like our PC! ^-^ ). Remember this concept here. The specific operation method will be described later when writing the MIDlet program.
Cldc class library API
Unless strictly stated, the cldc class library is a small subset of j2se. The following approximation is true: j2se contains the CDC including cldc. Note that cldc is not completely included in j2se, and it also includes classes dedicated to wireless facilities. Therefore, we can briefly divide the cldc class library into two parts: the classes inherited from j2se and the proprietary classes of cldc.
1.Classes inherited from j2se
This part includes three packages: Java. Lang, java. util, and Java. Io. Note that even those classes inherited from j2se are also greatly "scaled down. For example, java. util classes and interfaces are reduced from 47 of j2se to 10. The other two are also reduced accordingly.
System Type
From Java. LANG:
Object, class, runtime, system, thread, runnable,
String, stringbuffer, throwable
Data Type
From Java. LANG:
Boolean, byte, short, integer, long, Character
Container Type(Container class)
From Java. util:
Vector, stack, hashtable, enumeration
I/O class
From Java. IO:
Inputstream, outputstream, bytearrayinputstream,
Bytearrayoutputstream, datainput, dataoutput,
Datainputstream, dataoutputstream, reader, writer,
Inputstreamreader, outputstreamwriter, and printstream
Date and Time
From Java. util:
Calendar, date, timezone
Additional category
Java. util. Random, java. Lang. Math
Exception(Exception classes)
From Java. LANG:
Exception, classnotfoundexception,
Illegalaccessexception, instantiationexception,
Interruptedexception, runtimeexception,
Arithmeticexception, arraystoreexception,
Classcastexception, illegalargumentexception,
Illegalthreadstateexception, numberformatexception,
Illegalmonitorstateexception, indexoutofboundsexception,
Arrayindexoutofboundsexception,
Stringindexoutofboundsexception,
Negativearraysizeexception, nullpointerexception,
Securityexception
From Java. util:
Emptystackexception, nosuchelementexception
From Java. IO:
Eofexception, ioexception, interruptedexception,
Unsupportedencodingexception, utfdataformatexception
Error Type
From Java. LANG:
Error, virtualmachineerror, outofmemoryerror
2.Cldc proprietary class
Here Sun provides the javax. microedition. Io package. It is used to provide a bridge for communication with the outside world. (Note: In j2se, we use java.net. *) Note that all connections are created by javax. microedition. connector. For example:
You can open an HTTP connection via connector. Open ("http://www.chinajavaworld.com ")
Javax. microedition. Io interfaces include:
Connection, inputconnection, outputconnection,
Streamconnection, contentconnection, datagramconnection,
Streamconnectionnotifier
Here, you can see that the class libraries included in cldc are not enough to develop applications. For example, the UI is not supported. This is also the design concept of cldc. Cldc itself never imagined to solve all the problems. These problems must be solved by profile-MIDP on the upper layer. For example, javax. microedition. lcdui is the UI package defined in MIDP.
Features of devices corresponding to MIDP
Because mid devices have many limitations on the screen, memory, processor, and other issues, developing applications on mobile phones, PDAs, and other mid must consider some special technical points.
The following describes some features of the MID device:
- Display: 96x54 (minimum screen size), 1 bit (minimum color depth, monochrome)
- Input Device: one-handed keyboard (ITU-T keyboard), two-handedkeyboard (standard keyboard, QWERTY keyboard), touch screen.
- Memory Allocation: KB (MIDP component); 8 KB (persistent data generated by the application. For more information about persistent data, I will describe rms in detail in the future, here is a concept); 32 KB (Java Runtime Environment)
- Network: bidirectional, wireless, intermittent, and bandwidth-limited network
- Kernel: At least KVM must be run.
There are also many software features, such as reading and writing non-volatile memory (that is, the memory without losing content after power loss, such as flash ). Read and Write wireless device interface APIs, and so on. In addition to appeal technical issues, you must note that your program is easy to use, stable, and reliable. Especially for reliability, you have developed communication devices. Users cannot bear the possibility of program leaks that affect calls. Keep this in mind.
MIDP class library API
As described above, Sun defines the MIDP (mobile information device pro-file) layer on top of cldc to provide the UI and persistinace Storage) and higher-level network (compared with cldc) support. Let's take a look at the MIDP class library.
MIDP consists of four javax. microedition packages, including:
- Javax. microedition. RMS about permanent storage media (Note: RMS is short for record management system)
- Javax. microedition. MIDlet defines the MIDlet framework and interaction between the MIDlet and the environment.
- Javax. microedition. IO network support
- Javax. microedition. lcdui (User Interface) (Note: The UI is divided into high-level and low-level APIs .)
Note: If the language and runtime class (Java. Lang and Java. util) are added, there are six.
Write and run a MIDlet
Introduction to midplet
The application defined in MIDP is called the MIDlet. Any MIDlet is a subclass of javax. microedition. MIDlet. MIDlet and must inherit from javax. microedition. MIDlet. MIDlet. This is obvious. We have compiled an applet in j2se, And the applet must inherit from Java. Applet. applet. Is it very similar. The Inheritance System of the MIDlet is described.
Figure 4 inheritance system of MIDlet
A simple MIDlet
// helloworld. Java, a simple MIDlet program. Import javax. microedition. MIDlet. *; Import javax. microedition. lcdui. *; public class helloworld extends MIDlet implements commandlistener { private command exitcommand; private textbox TB; Public helloworld () { exitcommand = new command ("exit", command. exit, 1); TB = new Textbox ("Hello MIDlet", "Hello, world! ", 15, 0); TB. addcommand (exitcommand); TB. setcommandlistener (this); } protected void Startapp () { display. getdisplay (this ). setcurrent (TB); } protected void pauseapp () { } protected void destroyapp (Boolean U) { } Public void commandaction (command C, displayable d) { If (C = exitcommand) { destroyapp (false); policydestroyed (); }< BR >} |
The program displays the "helloworld" string. The program itself is very simple. If you are familiar with Java Applet programming, you will find it very similar to applet. Well, let's compile it and run it.
Preparations before Compilation:
- Hardware: because it is Java, the memory should be at least MB, and the CPU should be PII or higher.
- Platform: it is best to display Win2000 and Win98 in Chinese, which is difficult to solve. I have not tried Linux, but I think it should be okay with English, but it may be troublesome to show Chinese characters. If you have a friend who has tried it, please tell me.
- SDK: you need it (the following software can be downloaded for free on the sun website)
1. Java 2 SDK 1.3 or later.
2. j2e-wireless toolkit 1.0.3
Optional values:
Sun's ide: forte for Java. (If you are on broadband or you are not paying for your own network, ^-^)
After the download, install jdk1.3 first. (I will not detail the installation and configuration details. I want to know it clearly. If you do not know, please refer to the relevant information .) After you install the j2_wireless toolkit 1.0.3, it will automatically find your jdk1.3 and make relevant settings. If you do not need a command line, you can directly run the environment variables on the GUI without setting them, which is very convenient. For more information about how to set environment variables, see the manual.
Compilation steps
If you have installed JDK and j2mewtk as described above, follow these steps to compile the SDK:
(Note: The following JDK is installed in c: \ jdk1.3, And the j2wirless toolkit is installed in C: \ j2mewtk \)
1. Start-> Program-> j2wirless toolkit 1.0.3-> ktoolbar
2. The "j2wirless toolkit" window appears. Click "new project" and enter helloworld in project name; helloworld in MIDlet class name. Click creat project.
3. the setting for project dialog box appears. Use the default setting. Click OK.
Note that there is no sun IDE, so you need to manually place the helloworld. Java you have edited in advance under the c: \ j2mewtk \ apps \ helloworld \ SRC \ directory. Press the "build" key. If everything is normal, the compilation is completed. In this case, select the device you want from the device drop-down list and press the "run" button. You can select several different devices.
Lifecycle of MIDlet
We have compiled and run a simple MIDlet program. Now let's analyze the structure of the MIDlet and understand its lifecycle.
By observing the source program, we can find that the running of the MIDlet program is controlled by Startapp (), pauseapp (), and destroyapp. They are stored in javax. microedition. MIDlet
. MIDlet. All MIDlet must have these three methods.
As the name suggests, the Startapp () method is used to mark the start of execution of a MIDlet. Note that it is different from the constrctor of the helloworld program. Startapp () is not only executed when a MIDlet is initialized, as long as the MIDlet is activated (changed to active) from paused state, Startapp () will be called.
The pauseapp () method indicates that the MIDlet enters the pause state. The destroyapp () method indicates that the MIDlet enters the destroyed state. (Note: strictly speaking, it should be said that the method is called and a successful return marks ...)
After reading the above description, everyone may be confused. It is a method and a state. What is it? This should be explained from the execution mechanism of the MIDlet.
The execution of the MIDlet is managed by application management software. This is the general term for managing the underlying mechanism of MIDlet operations at the operating system level. The so-called MIDlet state is the one that it operates and controls and manages. The MIDlet State ensures that AMS can eliminate the MIDlet at any time, And the MIDlet can also enter a pause state and be activated again.
The MIDlet state can be divided into three types: paused, active, and destroyed. When AMS creates a new MIDlet object, it corresponds to the MIDlet, and its constructor is called and enters the paused state. After all the preparations are completed, AMS determines that the MIDlet can run now, and calls the MIDlet. Startapp () method. Enter the active state. When AMS decides to transfer the MIDlet to the paused state, it will call the MIDlet. pauseapp () method, and the MIDlet will pause the execution. Generally, the paused state will be used to release the occupied resources. When AMS determines that the MIDlet is no longer needed, it will call MIDlet. destroyapp () and the MIDlet will be eliminated.
Please note that I am talking about how AMS controls the status change of the MIDlet from the perspective of AMS. Programmers can also change the status of the MIDlet by calling the resumerequest, yypaused, and yydestroyed methods.
Our helloworld program first sets the unconditional value of destroyapp () to false and throws a midletstatechangeexception, indicating that the MIDlet does not want to be destroy at this time. Notitydestroyed () notifies amsmidlet to enter the destroyed state. For details, see midp api documentation.
The following describes the simplest state process of a MIDlet:
// Flowmidlet. Java MIDlet's simplest State Flow Import javax. microedition. MIDlet .*; Public class flowmidlet extends MIDlet { Public void Startapp (){ System. Out. println ("in Startapp ..."); Pauseapp (); } Public void pauseapp (){ System. Out. println ("in pauseapp ..."); Destroyapp (true ); } Public void destroyapp (Boolean unconditional ){ System. Out. println ("in destroyapp ..."); } } |
Changes to the MIDlet status can be expressed as follows:
Let's talk a few more things here. Concerning AMS, its role is not only to control the running status of the MIDlet. It is actually responsible for the entire running mechanism of the MIDlet. For more information about AMS, see instructor Wang Sen's article "using Java to write mobile apps-Java application manager" (programmer, 12th, 2001 ). Note that jam is AMS.
Conclusion
well, the introduction of j2_cldc/MIDP is over. Converting a post into HTML is a very tiring job. I have tried this before I understand this truth. If you have seen it and it is helpful, it will be my biggest reward. If you think there are any mistakes or feelings in the article, please feel free to email me.