非 JSR-75 设备上的 JSR-75 特定代码?
我想要做什么
我正在编写 J2ME midlet,并且我想使用 JSR-75 来写入文件。我还希望能够在不支持 JSR-75 的设备上运行我的应用程序。
我是如何做到的
我发现一个网站解释了如何做到这一点(抱歉忘记了 URL):
- 创建一个公开所有功能的公共抽象类(“服务”)。
- 创建一个扩展此抽象类的包私有类(“ServiceImplementation”),实现所有功能。
- 将两者放在一个单独的包中,将 Service 呈现为包的外观。
要实例化此类,请使用抽象类中的以下方法:
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;
}
}
出了什么问题
当 JSR-75 存在时,这非常有效。问题是我希望这个 midlet 也能在非 JSR-75 设备上运行,并且当我尝试这样做时,此代码会抛出 ClassNotFoundException: javax/microedition/io/file/FileConnection,即使我捕获了所有异常。
我已经进行了项目范围的搜索,以确保除了 Service 和 ServiceImplementation 之外,我没有在其他任何地方使用 FileConnection。
有谁知道我应该怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对你正在做的事情有点困惑,尤其是不同包中的两个类都称为服务!但我在编写在带有或不带有某些 JSR 的手机上运行的代码时遇到了这个问题。
您是否在服务类中引用 JSR-75 类?我怀疑即使您没有实例化 Service 类,它仍然会被加载。然后 JVM 就会遇到这些不存在的类。
将所有对 JSR-75 类的引用移至从 Service 类引用的类中。
Slightly confused about what you're doing, especially with two classes in different packages both called Service! But I have encountered this problem when writing code to run on handsets both with and without certain JSRs.
Are you referencing JSR-75 classes in your Service class? I suspect that even though you're not instantiating the Service class, it's still being loaded. The JVM is then coming across these classes which are not present.
Move all references to JSR-75 classes into a class which is referenced from the Service class.
显然我可以使用这个:
如果返回 null,则不支持 JSR-75。如果它返回任何其他内容,那就是。
Apparently I can use this:
If that returns null, JSR-75 isn't supported. If it returns anything else, it is.