Researched by Eric D. Larson
August 2002
Question
How can I share classes among multiple MIDlets?
Answer
In a J2SE environment you can arrange for multiple applications to share a third-party library (an XML parser for instance), in the form of a JAR file or class files, simply by adding them to the CLASSPATH. In the J2ME realm, class sharing isn't as easy.
- The MIDlet model bars access to external classes for a sound security reason: to prevent loading of malicious classes. MIDlets can access only the code actually included in their MIDlet suite.
Except for the MIDP API classes and vendor-specific extensions, any JARs or classes you wish to use you must include as parts of your MIDlet suite, by placing them in its /lib directory.
- Multiple MIDlets can share code if they're part of a single MIDlet suite. Unfortunately, many devices limit the size of MIDlet suite they'll accept, so in some cases you may have to bundle each MIDlet in its own suite, with its own copy of the shared code. Before falling back on this last resort, see whether an obfuscator will reduce the suite to an acceptable size.
- MIDlets not in the same suite can share classes in the development environment. If you use J2ME Wireless Toolkit 1.0.4, you can place JAR files in the /apps/lib directory. (Note that they're not packaged automatically.) This approach is useful when you're working with a group of devices and need to use vendor-specific APIs.
Acknowledgments
Thank you to members Rob Agar, Thomas Taylor, and David Lemon for answering this question on the KVM-INTEREST list.
Back To Top
|
|