To the experienced Java developer, J2ME (the Java 2 Micro Edition) looks just familiar enough to be tempting, but just different enough to warrant caution. J2ME in a Nutshell provides the extra security you need when venturing into programming for cell phones, PDAs and other consumer electronic devices. It gives you the reference material you need for J2ME, together with a tutorial that leverages your existing knowledge and teaches you what is different about J2ME without boring you to tears with details you already know. J2ME in a Nutshell provides a solid, no-nonsense reference to the "alphabet soup" of micro edition programming, covering the CLDC, CDC, KVM and MIDP APIs. The book also includes tutorials for the CLDC, KVM, MIDP and MIDlets, MIDlet user interfaces, networking and storage, and advice on programming small handhelds. Combined with O'Reilly's classic quick reference to all the core micro-edition APIs, this is the one book that will take you from curiosity to code with no frustrating frills in between Cover 1 Table of Contents 3 Preface 8 Contents of This Book 8 Related Books 10 J2ME Programming Resources Online 11 Examples Online 12 Conventions Used in This Book 12 Request for Comments 13 Acknowledgments 13 I: Introduction to the Java 2 Micro Edition Platform API 15 1. Introduction 16 1.1 What Is the J2ME Platform? 16 1.2 J2ME Specifications 21 1.3 J2ME and Other Java Platforms 21 2. The Connected Limited Device Configuration 23 2.1 The CLDC Java Virtual Machine 23 2.2 The CLDC Class Libraries 31 2.3 KVM Debugging 39 2.4 Advanced KVM Topics 44 3. The Mobile Information Device Profile and MIDlets 54 3.1 MIDP Overview 54 3.2 The MIDP Java Platform 58 3.3 MIDlets and MIDlet Suites 58 3.4 MIDlet Execution Environment and Lifecycle 64 3.5 Developing MIDlets 69 3.6 Delivery and Installation of MIDlets 83 4. MIDlet User Interfaces 91 4.1 User Interface Overview 91 4.2 The High-Level User Interface API 95 5. The Low-Level MIDlet User Interface API 146 5.1 The Canvas Class 146 5.2 Painting and the Graphics Class 150 5.3 Graphics Attributes 151 5.4 Drawing Lines and Arcs 154 5.5 Translating the Graphics Origin 160 5.6 A Simple Animation MIDlet 162 5.7 The Graphics Clip 165 5.8 Rendering Text 168 5.9 Images 173 5.10 Event Handling 178 5.11 Multithreading and the User Interface 183 6. Wireless Java: Networking and Persistent Storage 186 6.1 A Networking Architecture for Small Devices 186 6.2 Sockets 190 6.3 Datagrams 194 6.4 HTTP Connections 199 6.5 Persistent Storage 214 7. The Connected Device Configuration and Its Profiles 234 7.1 The CDC 234 8. J2ME Command-Line Tools 246 8.1 cvm: The Connected Device Configuration Virtual Machine 246 8.2 kdp: The KVM Debug Proxy 251 8.3 kvm: The Kilobyte Virtual Machine 253 8.4 midp: The MID Profile Execution Environment 257 8.5 emulator: The J2ME Wireless Toolkit Emulator 261 8.6 preverify: The KVM Class Preverifier 265 8.7 MakeMIDPApp: JAD to PRC Conversion Tool 268 8.8 chMEKeyTool: Public Key Certificate Management Tool 271 9. J2ME Programming Environments 274 9.1 The J2ME Wireless Toolkit 274 9.2 MIDP for PalmOS 288 9.3 J2ME and Forte For Java 298 9.4 Other Integrated Development Environments 303 II: API Quick Reference 305 10. J2ME Packages and Classes 315 10.1 J2ME Packages 315 10.2 J2SE Packages Not Present in J2ME 316 10.3 J2ME Package Contents 317 11. java.io 332 Package java.io 332 ByteArrayInputStream 332 ByteArrayOutputStream 334 DataInput 334 DataInputStream 335 DataOutput 336 DataOutputStream 337 EOFException 338 InputStream 339 InputStreamReader 340 InterruptedIOException 341 IOException 341 OutputStream 342 OutputStreamWriter 343 PrintStream 344 Reader 345 UnsupportedEncodingException 346 UTFDataFormatException 346 Writer 346 12. java.lang 348 Package java.lang 348 ArithmeticException 349 ArrayIndexOutOfBoundsException 350 ArrayStoreException 350 Boolean 350 Byte 351 Character 352 Class 353 ClassCastException 354 ClassNotFoundException 354 Error 355 Exception 355 IllegalAccessException 356 IllegalArgumentException 356 IllegalMonitorStateException 357 IllegalStateException 357 IllegalThreadStateException 358 IndexOutOfBoundsException 358 InstantiationException 359 Integer 359 InterruptedException 360 Long 361 Math 362 NegativeArraySizeException 362 NullPointerException 363 NumberFormatException 363 Object 363 OutOfMemoryError 365 Runnable 365 Runtime 366 RuntimeException 366 SecurityException 367 Short 367 String 368 StringBuffer 370 StringIndexOutOfBoundsException 372 System 373 Thread 374 Throwable 375 VirtualMachineError 376 13. java.util 377 Package java.util 377 Calendar 378 Date 380 EmptyStackException 381 Enumeration 381 Hashtable 382 NoSuchElementException 383 Random 383 Stack 384 Timer 385 TimerTask 386 TimeZone 386 Vector 387 14. javax.microedition.io 390 Package javax.microedition.io 390 Connection 391 ConnectionNotFoundException 392 Connector 392 ContentConnection 394 Datagram 395 DatagramConnection 396 HttpConnection 398 InputConnection 400 OutputConnection 401 StreamConnection 401 StreamConnectionNotifier 402 15. javax.microedition.lcdui 403 Package javax.microedition.lcdui 403 Alert 405 AlertType 406 Canvas 407 Choice 409 ChoiceGroup 411 Command 412 CommandListener 414 DateField 414 Display 415 Displayable 416 Font 417 Form 419 Gauge 420 Graphics 421 Image 424 ImageItems 425 Item 426 ItemStateListener 427 List 428 Screen 429 StringItem 430 TextBox 431 TextField 431 Ticker 434 16. javax.microedition.midlet 435 Package javax.microedition.midlet 435 MIDlet 435 MIDletStateChangeException 437 17. javax.microedition.rms 439 Package javax.microedition.rms 439 InvalidRecordIDException 439 RecordComparator 440 RecordEnumeration 441 RecordFilter 443 RecordListener 444 RecordStore 445 RecordStoreException 448 RecordStoreFullException 448 RecordStoreNotFoundException 449 RecordStoreNotOpenException 449 Class, Method, and Field Index 450 A 450 B 452 C 454 D 458 E 462 F 465 G 467 H 478 I 484 J 490 K 490 L 492 M 494 N 497 O 499 P 501 R 504 S 509 T 519 U 522 V 523 W 523 Y 525 Colophon 526 This guide to J2ME offers the extra security needed when venturing into programming for cell phones, PDAs and other consumer electronic devices. It contains the reference material needed for J2ME, together with a tutorial that should leverage existing knowledge and teach what is different about J2ME. It is a solid reference to the alphabet soup of micro edition programming, covering the CLDC, CDC, KVM and MIDP APIs. It also includes tutorials for the CLDC, KVM, MIDP and MIDlets, MIDlet user interfaces, networking and storage, and advice on programming small handhelds