Skip to main content

Will a MIDP2 packaged class library run on a MIDP1 phone?

1 reply [Last post]
davjoh
Offline
Joined: 2007-07-18
Points: 0

Hi everyone,

Slightly random question, but will a MIDP2 packaged class library run on a MIDP1 phone?

Basically, I'm creating a class library which ideally I'd like to reuse in both MIDP1 and MIDP2 applications. But I'd rather not have to create 2 separate compatible class libraries if you get my meaning.

So in my library I would have a runtime check for what platform I'm running, then avoid the classes that are not compatible (i.e. the MIDP2 classes that aren't implemented in MIDP1 and therefore won't run in MIDP1).

I know it sounds a bit backward-thinking since MIDP1 is old stuff, but there's a lot of old phones out there, including recycled ones in the third World that can still be targetted.

Ideas, comments welcome!

-David

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sfitzjava
Offline
Joined: 2003-06-15
Points: 0

Yes it can be done.

String profile = System.getProperty("MicroEdition-Profile");

if(profile.equals("MIDP-2.0")
{
PersistEngine pe = Class.forName("PersistMidp2");
}else
{
PersistEngine pe = Class.forName("PersistMidp1");
}

So in the above code you get the JAD setting for the profile to determine which set of classes to load. Then using an interface you define an object, then load the class by name. This is the only way because the compiling of the library into 3rd party apps it must have a reference to all your classes, and you will not want the MIDP1 builds to have any of the MIDP2 classes or they will not verify correctly.

Another point is you will need to subclass the MIDP2 classes you want to use with an interface, or have a dummy class for the MIDP1 build. Meaning you can't use a sprite class but rather need a wrapper class that extends or incapsulates these higher level classes and a dummy set for MIDP1 that do nothing, or the wrapper uses an interface then only the interface needs to be in the MIDP1 library jar.

Class MySpriteImpl extends Sprite implements MySprite

if(MIDP2)
{
MySprite msp = Class.forName("MySpriteImpl");
}else
{
// in midp1 there are no sprites but the above code will
// still need to compile in midp1. Since the MySpriteImpl
// uses an interface of MySprite, that is all that is needed
// in the midp1 jar for a successful build.
}

For an example of this in the real world, look at my project
microbus.dev.java.net in the source I have built this library
to not only run on MIDP but also Java SE. In the case here
I have to use a Vector in MIDP, but in JavaSE I would rather
use a HashMap, for better performance. In my code I try to
load a class that is only available in MIDP, and thus if it throws
an exception then I must be in JavaSE.

Regards,
-Shawn