TechBuzzPro

How to Print Hello World in J2ME on a Phone

· 6 min read · Phones & Tablets

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To print "Hello World" in J2ME, you write a MIDlet class that extends javax.microedition.midlet.MIDlet, create a Form or TextBox containing a StringItem with the text, and display it with Display.getDisplay(this).setCurrent(...) inside startApp(). You then compile it against a J2ME/MIDP SDK, preverify the class files, package them into a .jar with a matching .jad descriptor, and install that JAR on a phone that still supports MIDP. Nothing about the code is difficult; the work is in getting a toolchain that targets the old Java ME platform and a device or emulator that can run it.

What You'll Need

  • A computer with a Java Development Kit. Older JDKs (8 or 11) are the least painful because the Java ME tooling predates the modular JDK changes.
  • A J2ME development kit that includes MIDP and CLDC libraries. The classic choice is the Oracle/Sun Java ME SDK or the older Sun Java Wireless Toolkit (WTK). Either bundles an emulator.
  • An editor or IDE. NetBeans with the Mobility plugin is the traditional route; a plain text editor plus command-line tools also works.
  • A target device: the bundled emulator, or a real phone with MIDP support and a way to transfer a JAR to it (USB, memory card, or Bluetooth).
  • The JAR and JAD files you will build, plus a signing key only if the phone requires signed MIDlets to install.

Step-by-Step

  1. Create a project folder with a source directory, for example HelloMidlet/src, and a build output directory such as HelloMidlet/bin. Keeping source and output separate makes the packaging step predictable.
  2. Write the MIDlet class. Create HelloMidlet.java in the source folder. It must extend MIDlet, implement startApp(), pauseApp() and destroyApp(boolean unconditional), and set the display in startApp(). A minimal version builds a Form, appends a StringItem holding "Hello World", and calls Display.getDisplay(this).setCurrent(form). Import javax.microedition.midlet.MIDlet, javax.microedition.lcdui.Display, Form and StringItem.
  3. Declare the MIDlet in the manifest. The JAR manifest needs a MIDlet-1 attribute in the form Name, Icon, ClassName, for example Hello, , HelloMidlet. Without this line the phone installs the JAR but shows no application to launch.
  4. Compile against the MIDP and CLDC libraries. Point javac at the midp.jar and cldc.jar (or cldcapi/midpapi JARs) from your SDK with -bootclasspath, and set the source and target level to something the old platform accepts, typically 1.4 or 1.3. Compiling against a modern desktop JDK without these libraries fails because the javax.microedition packages do not exist there.
  5. Preverify the classes. Run the SDK's preverify tool over the compiled class files, outputting to a separate directory. MIDP devices expect the extra stack map attributes that preverification adds; a JAR built from unverified classes is rejected at install time on many handsets.
  6. Package the JAR. Zip the preverified class files plus the manifest (and any resources) into HelloMidlet.jar, with the manifest as the first entry. Use the SDK's packaging tool or the jar command with an explicit manifest file.
  7. Write the JAD descriptor. The JAD lists MIDlet-Name, MIDlet-Version, MIDlet-Vendor, MIDlet-Jar-URL, MIDlet-Jar-Size and the same MIDlet-1 line as the manifest. The JAR size must match the actual file size in bytes, or installation fails.
  8. Run it in the emulator first. Launch the SDK's emulator, point it at the JAD or JAR, and confirm the form appears with "Hello World". This isolates code problems from device problems before you touch a handset.
  9. Install on the phone. Copy the JAR (and JAD if the phone wants both) to the device by USB or memory card, then open it from the phone's file manager or application installer. Accept the installation prompt. Launch the app from the phone's application menu; the form with the text should appear.

Common Mistakes

The most frequent failure is a missing or malformed MIDlet-1 manifest attribute, which produces an installed but unlaunchable app. The second is skipping preverification, which causes an install error on stricter handsets even though the emulator runs the same classes fine. A third is a JAD whose MIDlet-Jar-Size does not match the real JAR, which aborts installation. Compiling against a desktop JDK without the MIDP libraries, or forgetting to implement all three lifecycle methods, also stops the build or the launch. Finally, putting UI code in the constructor instead of startApp() can throw an exception on some devices because the display is not ready yet.

Tips

Keep the first MIDlet as small as possible: one class, one form, one string. Once that installs and runs, add features. Test in the emulator with the same screen size and MIDP version as your target phone, because layout and font handling differ. Keep the JAR name and the class name consistent with what the manifest declares. If the phone supports it, sign the MIDlet with a certificate the handset trusts; unsigned apps trigger repeated permission prompts or are blocked outright. Store the build commands in a small script so recompiling, preverifying and repackaging is one step rather than three manual ones.

Troubleshooting

If the emulator shows a blank screen, check that setCurrent() is actually called in startApp() and that the Form has the StringItem appended before it is displayed. If compilation fails with missing javax.microedition packages, your bootclasspath does not point at the MIDP and CLDC JARs. If installation on the phone fails, compare the JAD's MIDlet-Jar-Size with the file size, confirm the JAR contains preverified classes, and check that the manifest's MIDlet-1 class name matches the actual class. If the app installs but does not appear in the menu, the manifest attribute is missing or the JAD and manifest disagree. If the phone refuses the file entirely, it may require a signed MIDlet or may not support the MIDP version you targeted; try a lower CLDC/MIDP profile or a different device.

Frequently Asked Questions

Can I still run J2ME apps on a modern phone?

Not natively. Android and iOS do not include a MIDP runtime. J2ME code runs on old feature phones, on the SDK emulator, or through third-party emulators that reimplement the platform; those emulators are separate apps with their own compatibility limits.

Do I need the JAD file, or is the JAR enough?

Many phones install from the JAR alone, but some require the JAD for metadata such as size and permissions. Supplying both is the safer choice, and the two must agree on the MIDlet attributes.

Why does the emulator run my app but the phone rejects it?

Almost always preverification, signing, or a size mismatch in the JAD. The emulator is more forgiving than real handset installers.

What is the difference between CLDC and MIDP?

CLDC is the connected limited device configuration that defines the core Java subset and virtual machine; MIDP is the mobile information device profile that adds the UI, storage and networking APIs. A MIDlet needs both.

Can I use a modern JDK to build this?

You can compile with a recent JDK if you supply the old MIDP and CLDC libraries on the bootclasspath and target an old source level, but older JDKs cause fewer tooling problems because the Java ME SDKs were built for them.

Where does the text actually get drawn?

On a Form or TextBox, the platform renders the StringItem or text content using the device's own UI. You are not drawing pixels unless you use a Canvas and Graphics.drawString().