Latest Entries »

Wednesday, August 19, 2009

How To Install & Run Java On iPhone

iPhone is a great phone to develop applications for. Unfortunately Apple decided to restrict developing iPhone applications in a less known language called Objective C, which is used pretty much within Apple (and by Apple developers) and almost nowhere else. Only Steve Jobs knows the reason behind this strange decision. Java (J2ME) is the most popular language & platform for mobile development. So Java developers need a way to develop applications for iPhone too by leveraging their core competency.

Apple's SDK for the iPhone, as you know, is based on Objective-C as the development language as well as Cocoa for the GUI.

Unfortunately Apple's super-restrictive license agreement for the iPhone SDK prohibits the porting of the Java virtual machine to the iPhone. Today we will talk about how we can use Open Source Java to run applications which will run on Apple's iPhone. The open source project by Arno Puder, Associate Professor at the San Francisco State University, uses a cross-compiler to convert Open Source Java code to Objective-C and provide a Java-based implementation of the Cocoa library. With the help of these tools, iPhone applications can be written in pure Java.

Using the Java version of Cocoa, it is possible to run a Java-based iPhone application as a Java desktop/applet application that can be cross-compiled to run natively on the iPhone.

AlcheMo-for-iPhone - Automated J2ME to iPhone porting solution
alcheMo for iPhone contains translator to convert J2ME application source code to equivalent C++ source code for iPhone.

No manual adjustments to the translated source code are required. Compiled using the standard Xcode toolchain and linked with alcheMo's optimized run-time library, a native iPhone application is produced.

alcheMo for iPhone is capable of converting J2ME applications utilizing an extensive subset of Java ME CLDC 1.1 and MIDP 2.0 (including touch screen support) and supports several JSR extension APIs including the JSR-256 mobile sensor API. This automatic translation process is instantaneous, repeatable and doesn't require iPhone specific experience.

Garbage collection and automatic memory management is one of the strengths of the J2ME environment. Unlike on Macintosh OS X, Objective C on iPhone does not support garbage collection. By incorporating an advanced garbage collector, alcheMo eliminates the need for manual memory management. Whole classes of common programming errors such as dangling pointers are thus prevented.

The initial version of alcheMo for iPhone is optimized for mobile games.

The bad news is that their beta program has closed on April 24th, while they are preparing for commercial release. alcheMo for iPhone has backing from Sun Microsystems.

BTW: During Java One 2008, Sun officials repeatedly mentioned that they successfully ran Java on iPhone but are thwarted only by Apple's licensing restrictions in publicly announcing it. alcheMo circumvents Apple's licensing restrictions with their language translator.

Using Java on Unlocked & Jailbraked iPhone with Installer

Note: The legality of the procedure below is dubious. IANAL.

First you need to unlock and jailbrake you iPhone. You can use the windows user interface for ZiPhone to jailbreak, unlock and activate any verion of iPhone.

Then you need to have the installer app on iPhone. is a UIKit based package manager for the iPhone. It works by downloading packages over WiFi (wireless networking) or EDGE. It supports installing, updating and uninstalling applications from multiple sources.

winpwn can simplify the above steps for you.

Now go to Installer and install Mobile Terminal and Cydia Installer. Restart iPhone.

Run Cydia Installer and go to Java section and select iPhone/Java which will install the virtual machine, libraries etc. Then install Jikes (java compiler). Now restart iPhone again.

That's it, you are done.

How to load, compile & run Java applications on iPhone
First install Java on iPhone following instructions in the section above before reading this.

You can upload Java files to iPhone using iPhone Browser.

You can run the class file as usual:
java MyFirstJavaProgramForIPhone

Java on iPhone - Concluding thoughts
The second method (using jailbraked iPhone) suffers from three major problems.
1. To use your Java applications, your user will also have to jailbrake their iPhone. As such its usage will be limited to highly technical people who are not afraid of tinkering with their costly iPhone and potentially even risk damaging it permanently!

2. While it may be fine for experimenting, the legality is dubious. You may run into problem with Apple for selling or even distributing such applications.

3. The jailbraked versions of iPhone may not be compatible with future firmware updates, and Apple will try their best to disable them at every opportunity and had done so in the past.

All of the above makes method 2 unsuitable for commercial use.

Commercial J2ME developers have to wait for commercial release of alcheMo-for-iPhone. Innaworks is well known for porting standards compliant J2ME to BREW mobile platform. I expect their solution to be ready in 2-3 months time. If you do not have the time then download Apple's iPhone SDK and start coding in Objective C.
BTW: Make sure you read the iPhone SDK agreement carefully before you invest huge sums in iPhone development.

Run Terminal (installed above) and use java (jikes) compiler to compiler your program. For example:
jikes -cp /usr/lib/rt.jar


Mage said...

Hey, so I got JAVA setup OK, i can run basic println programs and such, but Im stuck as soon as I need to import libraries. I've only really worked with BlueJ which basically does all the library setup for you, but when I try to run programs that import Java.util or, it gives a noclassdeffounderror: java/util/Scanner. I was wondering where to download libraries and how to point java to them on my ipod touch. Thanks

Sravan Modugula said...

Hello Mage,
java.lang is the default package available in java, there is no need to import this package. If your using, java.util or some other package then you need to import this package in your java program first. I believe that you have installed the Java on your iPhone/iTouch, the installer consists of all the java related jar(packages) files. Possible mistakes: you might have missed import statement in your java program, java.lang.NoClassDefFoundError: it means your class path is not set properly.
Check these points, in case of any issue just post your comments. I will revert at the earliest.

Mage said...

Well I got the program to compile and run on my computer, and then copied the resulting class file to my ipod, but it won't run on my Ipod. Should I try reinstalling java completely?

Mage said...

So i tried copying the .java file instead of the .class file and compiling it on the phone. When I use the "jikes -cp /usr/lib/rt.jar" it gives me semantic errors most notably (Type "Scanner" was not found) so It must not be importing the java.util correctly. Any ideas? I'm trying to get keyboard input via the Scanner for a simple dice program I wrote. It uses Scanner and Random as well as Strings ints and doubles. Is there something I need to change when compiling? (jikes -cp /usr/lib/rt.jar Should I include anything else in the compile command? Thanks for you help


Sravan Modugula said...

Scanner class resides in java.util package, rt.jar consists of all the required java packages. One more thing rt.jar also consists of another Scanner class in com.sun.java_cup.internal.runtime which is different from java.util class. This may lead to class conflicts, i guess your old syntax would be like
Scanner s = new Scanner(;
change this to
java.util.Scanner s = new java.util.Scanner(;
Nothing to include in the command line, try this and let me know :)

Mage said...

Well, It's still not working. Maybe my programs's screwed up. Could you send me a simple program with a scanner that works on yours so I could see if my setup is bad? It's still giving me Semantic error (Type "Scanner was not found) and then also with the new syntax (Type "java.util.Scanner" was not found) Any other ideas?


Sravan Modugula said...

Try this simple example.

* This class read's input from console using Scanner

import java.util.Scanner;

* @author Sravan Modugula
public class ScannerToReadConsole {

* @param args
public static void main(String[] args) {

Scanner scanner = new Scanner(;

System.out.print("Enter your name: ");
String name = scanner.nextLine();

System.out.print("Enter your phone number: ");
int phoneNumber = scanner.nextInt();

System.out.println("Hey your name is , " + name + ".");
System.out.println("Your phone number is " + phoneNumber + ".");



This should work, if not then post your program and let me know the environment details in which your executing your program like iPhone firmware version and JDK.

Java on iPhone said...

Good one. We miss Java on iPhone officially. I wonder if apple allows that.

Mage said...

The above program still did not work. Strange. I extracted my rt.jar file just to check and it does indeed include the util and io information. I am running OS 3.0 on an Ipod touch 2g, maybe the fact that it is 3.0 is throwing off some sort of path information? Another thing, I just started using netbeans which creates a jar file when one builds their program. Just wondering, is there a way to use the jar file instead of the .class file? For example using test.jar instead of test.class? Thanks


Sravan Modugula said...

If your running a Java program on your iPhone which imports java.lang then it goes fine and when your import some other package it is throwing an error. Is that your case ?
To run the jar files on your iPhone you can try the following command
java -jar yourjarfile.jar

Mage said...

I believe thats the case, yes. I tried running a jar file as well using your suggestion, and it didn't throw any errors, but it didn't run the program either. I've tried uninstalling and reinstalling several times. Maybe I'll just have to wait until an update comes out.

Anonymous said...

I was wondering, was this version of the Java written for 2.2 firmware? Maybe that's my problem, although it finds the rt.jar file fine.


Sravan Modugula said...

There might be a possibility, coz in this case we are sure that this issue is related to the configuration.

Tony said...

Thought I would leave a comment about this in case others were looking for an answer.

But I imported the rt.jar file into Eclipse and starting looking through the jar file for the Scanner.class and couldn't find it anywhere. So if you want to read input you can use this declaration in order to get input from the user.

BufferedReader reader = new BufferedReader(new InputStreamReader(;

I don't know why they excluded Scanner but this does the same thing except when you want to get numbers, you'll have to parse them from the String after reading them in because Buffered Reader only reads in Strings.


Mage said...

Sweet! Thank you so much. That actually worked. Now, is there any way to add the scanner to the jar file? I tried extracting it and replacing it with the jar that came with my computer java, but that just caused errors. Thanks for all the help again,


SHYAM said...

Thank you for the info. It sounds pretty user friendly. I guess I’ll pick one up for fun. thank u

J2ME Developer

Minh Hoàng said...

I guess I’ll pick one up for fun. thank u wordpress themes for videos