What I'm trying to do
I'm coding a J2ME midlet, and I want to use JSR-75 to write files. I also want to be able to run my app on device that don't have support for JSR-75.
How I'm doing it
I found a website that explains how to do this (forgot the URL, sorry):
- Create a public abstract class ("Service") that exposes all functionality.
- Create a package-private class ("ServiceImplementation") extending this abstract class, implementing all functionality.
- Put both in a separate package, rendering Service the facade for the package.
To instantiate this class, the following method from the abstract class is used:
public static Service getInstance() {
try {
Class.forName("javax.microedition.io.file.FileConnection");
Class c = Class.forName("my.package.Service");
Service service = (Service) (c.newInstance());
return service;
} catch (Exception e) {
return null;
}
}
What goes wrong
This works perfectly when JSR-75 is present. The problem is that I want this midlet to run on non JSR-75 devices as well, and this code throws a ClassNotFoundException: javax/microedition/io/file/FileConnection when I try do do so, even though I'm catching all Exceptions.
I've done a project-wide search to ensure that I'm not using FileConnection anywhere else but in Service and ServiceImplementation.
Does anyone know how I'm supposed to do this?