MIDP

Compiling Java (SE, ME) in PocketPC with PhoneME

Posted on Updated on

As I promised earlier I try to find a free method to compile java sources on PDA including Java SE and Java ME. In my earlier post I succesfully compiled Java SE and ME code with CrE-ME.

How to compile

I’m used precompiled version of PhoneME as a JVM and cke as text editor.

To compile with sun.tools.javac.Main make some batch file (%1 is the fullname of the source, %2 is the fullpath):
Compile.bat

@echo \phoneme\foundation\bin\cvm -cp "\jar\tools.jar;\jar\rt.jar;%2\" sun.tools.javac.Main %1
@\phoneme\foundation\bin\cvm -cp "\jar\tools.jar;\jar\rt.jar;%2\" sun.tools.javac.Main %1

MidpCompile.bat

@echo \phoneme\foundation\bin\cvm -cp "\jar\tools.jar;\jar\rt.jar;\JAR\midpapi10.jar;\JAR\cldcapi10.jar;%2\" sun.tools.javac.Main %1
@\phoneme\foundation\bin\cvm -cp "\jar\tools.jar;\jar\rt.jar;\JAR\midpapi10.jar;\JAR\cldcapi10.jar;%2\" sun.tools.javac.Main %1

Run

You can compile and run your compiled class:

Compile \srcpath\hello.java \srcpath

\phoneme\foundation\bin\cvm \srcpath\hello

It seems to me that running the compiled MIDP is more compilcated, you should install your midlet *.jar first then run with the MIDPLauncher. You can install a midlet within the GUI, but it’s frustrating as you can’t use copy/paste, or this command line installer:

InstallMidlet.bat

if not %1X == X goto run_midlet
@echo off
echo .
echo Usage: installMidlet ^<URL^> [^<URL label^>]
echo .
goto :EOF

:run_midlet
cd \phoneme\midp\bin\arm\runMidlet -1 com.sun.midp.scriptutil.CommandLineInstaller I %1 %2

I believe somehow we can run our classes without jar’ing but how. However it seems to me that you can run your midlet from the command line only if you know it’s position in the MidletLauncher:

RunMidlet.bat

cd \phoneme\midp\bin\arm\runMidlet %1

Example: RunMidlet 2 will start the second installed MIDP.

isJavaLetterOrDigit(C) error

You may encounter a serius error while compiling Java ME source code:

java.lang.NoSuchMethodError: java.lang.Character: method isJavaLetterOrDigit(C)Z not found
at sun.tools.java.Scanner.scanIdentifier(Unknown Source)

In JavaDocs I found this:

public static boolean isJavaLetterOrDigit(char ch)
Deprecated. Replaced by isJavaIdentifierPart(char).

Determines if the specified character is a “Java” letter or digit, that is, the character is permissible as a non-initial character in an identifier in the Java language.

A character is considered to be a Java letter or digit if and only if it is a letter, a digit, the ASCII dollar sign character '$', or the underscore character '_'.

On this arcticle:

The pages http://jeffreyrfox.addr.com/zaurus/ and http://www.micronova.com/ZAURUS/index.html recommend using the compiler in the J2SE 1.1 classes to compile with sun.tools.javac.Main. For Java 1.3.1 that would be tools.jar, giving you

java -cp tools.jar sun.tools.javac.Main Unfortunately, that does not work with Java 1.3.1. The J2SE compiler uses Character.isJavaLetterOrDigit, which in J2ME has been replaced by Character.isJavaIdentifierPart. And since Sun does not distribute the java sources of their compiler, we can’t fix it.

Solution

Use the Eclipse‘s JDT Compiler named ecj.jar.
ecj.bat

@echo \phoneme\foundation\bin\cvm -jar \java\ecj\ecj.jar -cp \jar\midpapi20.jar;\jar\cldcapi11.jar;%2\ %1
@\phoneme\foundation\bin\cvm -jar \java\ecj\ecj.jar -cp \jar\midpapi20.jar;\jar\cldcapi11.jar;%2\  %1

Configuring cke to compile

PhoneMe

Program: \Windows\CMD.exe

Arguments: /c “\Compile.bat” “$fullname” \$filepath 1>”$filepath\$filename.log” 2>”$filepath\$filename.err”

[X] Save actual document

ECJ

Program: \Windows\CMD.exe

Arguments: /c “\ecj.bat” “$fullname” \$filepath 1>”$filepath\$filename.log” 2>”$filepath\$filename.err”

[X] Save actual document

FAQ

Q: Compiling with ecj.jar will cause an error

cannot read the source from <src.java> either the file uses a different than utf-8 or it is corrupted

A: Use only English characters (avoid á, é, ő, ú, ű, í etc even in commented lines)

Q: How to preverify all the classes? This process is necessary in case of MIDP.

A: I don’t know, maybe Eclipse has a equivalent java bytecode? Update: read this post!

Q: How to make jar and jad file?

A: I don’t know, maybe TotalCommander can zip, add manifest file and rename it. Edit jad manually.