Serializable interface on J2ME MIDP 1.0 device

T

Thomas Hawtin

I am part of a small J2ME projekt that has both a server side (J2EE)
and a client side (J2ME, MIDP 1.0). Now I have found a small problem,
when we share code on both sides. I have an class Article, which is
needed on both the mobile phone application, and on the server side.
Now, on the server side I need it to be serializable, but when I write
"implements java.io.Serializable" it won't compile on the client side
(using Sun's Wireless Toolkit). Apperently the interface
java.io.Serializable doesn't exist in in MIDP 1.0. To work around this,
we currently have two different Article classes, one for each side. But
it feels really stupid to have two practically identical classes. The
*ONLY* difference is that one implements java.io.Serializable. Is there
a better way? We would like to use a single class file for both sides.

The technique I've used is to use my own interface xyz.Serializable
instead of java.io.Serializable. The version of xyz.Serializable
compiled for J2SE/J2EE implements java.io.Serializable. The J2ME version
does not.

Alternatively you can use sed or something similar (there's a suitable
task in ANT) to remove the implements java.io.Serializable when
compiling for J2ME. Or equivalently, to add it when compiling for J2SE/J2EE.
First I thought that we could simple create the package java.io on the
J2ME side, with the interface Serializable. But what would happen if
the application is run on a MIDP 2.0 phone, that already has the
java.io.Serializable interface? It seems that it could cause some
trouble...

If the phone already has java.io.Serializable, then it will just use its
own copy. However, if it doesn't have it, then it will probably complain
about loading classes into java.*.

Tom Hawtin
 
J

jimi_usenet

I am part of a small J2ME projekt that has both a server side (J2EE)
and a client side (J2ME, MIDP 1.0). Now I have found a small problem,
when we share code on both sides. I have an class Article, which is
needed on both the mobile phone application, and on the server side.
Now, on the server side I need it to be serializable, but when I write
"implements java.io.Serializable" it won't compile on the client side
(using Sun's Wireless Toolkit). Apperently the interface
java.io.Serializable doesn't exist in in MIDP 1.0. To work around this,
we currently have two different Article classes, one for each side. But
it feels really stupid to have two practically identical classes. The
*ONLY* difference is that one implements java.io.Serializable. Is there
a better way? We would like to use a single class file for both sides.

First I thought that we could simple create the package java.io on the
J2ME side, with the interface Serializable. But what would happen if
the application is run on a MIDP 2.0 phone, that already has the
java.io.Serializable interface? It seems that it could cause some
trouble...

Note: the server side doesn't use serialization when sending data to
the mobile (it uses simple http text only for that).

Regards
/Jimi
 
J

jimi_usenet

Thomas said:
The technique I've used is to use my own interface xyz.Serializable
instead of java.io.Serializable. The version of xyz.Serializable
compiled for J2SE/J2EE implements java.io.Serializable. The J2ME version
does not.

That worked like a charm. Thanks for the tip, Tom!

/Jimi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top