| Reply | « Previous Thread | Next Thread » |
|
I'm trying to check for optional packages using the Class.forName method.
This is a standard way to do it (as presented in http://developers.sun.com/techtopics...cles/optional/): try { Class.forName( "javax.wireless.messaging.MessageConnection" ); // do something } catch( Exception e ){ // do something else } This works on some Nokia phones (e.g. 6230), but unfortunatly not on all. E.g. the 6310i or 7210 simply stop the midlet with "Cannot create class in system package". So does anybody know how Nokia wants me to do the check for optional packages on their phones? |
| Robert Reiner |
| View Public Profile |
| Find all posts by Robert Reiner |
|
Do you have any import statements that refer to those classes? If you find out that they exist how are you instantiating them? You can't have any reference to those classes in your code. You have to create them and do everything using the Class.forName() method.
shmoove |
|
Hello shmoove,
thank you for your reply. No, I have no import statements for those classes. I just want to check whether or not these classes are present on the device and give a message to the user. |
| Robert Reiner |
| View Public Profile |
| Find all posts by Robert Reiner |
|
OK. So what do you do when you say:
Code:
// do something |
|
So this is what I do:
final StringBuffer presentPackages = new StringBuffer(64); ... try { Class.forName(test); final int dot = test.lastIndexOf('.'); if (dot != -1) { if (presentPackages.length() != 0) { presentPackages.append(' '); } presentPackages.append(test.substring(0, dot)); } } catch (final Exception e) { // optional package not present on the device. } e.g. test = "javax.wireless.messaging.MessageConnection", in the buffer I collect the package names that are present on the device. |
| Robert Reiner |
| View Public Profile |
| Find all posts by Robert Reiner |
|
Robert Reiner
You can check your code for correct handling of the package optionality by running your app on some mobile that doesn't have these packages for sure. For example, you can use Sun supplied WTK simulators |
|
doctordwarf,
thank you for this information: I use emulators for the devices I mentioned in my first post and I know that they do not contain all the packages I test. The problem is, that some execute the code as I expect, but others quit with "Cannot create class in system package". I'm looking for the way to do it, that runs successfully on (at least) all Nokia devices... |
| Robert Reiner |
| View Public Profile |
| Find all posts by Robert Reiner |
|
Robert Reiner
This is pretty strange.. "Cannot create class in system package" actually means exactly that you are trying to work with some class which is not present in your JAR, nor in the phone.. I would advice the following: 1. Try identifying the problematic line of code. E.g. do some verbose output to see which code is really executed or comment all the code and uncomment it line by line 2. Maybe this is not directly fault from your code. Can it be so that you use some framework that uses some protocol name for instantiating something else... I can hardly imaging some situation, but just to mention the possibility 3. I use emulators for the devices I mentioned in my first post and I know that they do not contain all the packages I test If your code executes successfully on the Sun emulator, you'll be sure, that this is the bug of a paticular phone. If it fails on Sun' emulator, there are probably some bugs in your code |
|
doctordwarf,
thank you for your detailed reply. It seems that I cannot make my problem quite clear (so please give me another try): I want to test whether an optional package is present on a phone or not. I do not assume anything on the side of the phone: My code has to run even if the package I test does not exist (and it is expected that at least some of the packages I test do not exist). So it is a normal condition for the program that a requested class does not exist on the phone (neither in my jar, nor on the phone). According to your advices: 1: The problematic code is Class.forName("...") 2: I do not _think_ so, because it is "only" the forName() call. 3: My code works on the Sun emulator [Version 2.1 (11.12.2003 11:05)], so thanks to you advice I can be sure that my code is conform to the spec (if the emulator does not have a bug ;-) ). Non-the-less my code has to run on real Nokia devices (even if they have a bug). So I'm still looking for the Nokia-way to request a device to give information about its installed optional packages (as the 'standard' way does not work on all devices). |
| Robert Reiner |
| View Public Profile |
| Find all posts by Robert Reiner |
|
This sounds fantastic :)
Well. have one more... lets call it possible workaround advice :) Try checking not for "javax.wireless.messaging.MessageConnection" but for some "realshit.neverimaginable.packagename". Maybe your particular phones have these unrealistic problems only when trying to work with some (halfimplemented?) package. If it is so, you have the following workaround for buggy phones: check the phone model and if it is known for having bugs with "javax.wireless.messaging.MessageConnection", don't check for "javax.wireless.messaging.MessageConnection" at all |
|
Hey!
What does the following mean?! Code:
catch (final Exception e)
{
Can it be so, that it prevents the phone from handling your Exception? Try usual syntax: Code:
catch (Exception e) |
|
Hello doctordwarf,
thank you again for your ideas! Unfortunatly your first suggestion does not work since I do not know in advance what packages will be checked. But according to your suggestion I played a bit with the package names: Class-forName() is only a problem with non-existing classes in the java and javax.microedition packages. I assumed that all javax packages would not work, but e.g. javax.bluetooth.DataElement or javax.no.Bugs does not raise any problem. According to 'final': I use this modifier to indicate that the value will not change. I allway use it for catch-Blox and parameter declarations as well as local variables. But this is not the reason to my problem with the forName-Method. BTW: I tested th forName-Method with Siemens emulators with the same result as the one from Nokia: Some execute, some close the application. :-( |
| Robert Reiner |
| View Public Profile |
| Find all posts by Robert Reiner |
|
Well, then youäll have to hard-code list of packages for the problematic phones.. of course if there is only a couple of such phones
|
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|