Serializable interface on J2ME MIDP 1.0 device

Discussion in 'Java' started by Thomas Hawtin, Mar 29, 2006.

  1. wrote:
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Mar 29, 2006
    #1
    1. Advertising

  2. Thomas Hawtin

    Guest

    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
    , Mar 29, 2006
    #2
    1. Advertising

  3. Thomas Hawtin

    Guest

    Thomas Hawtin wrote:
    >
    > 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
    , Mar 29, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page