You Are Here:

Community: Developer Discussion Boards

#1 Old Checking for optional packages - 2004-04-25, 02:00

Join Date: Apr 2004
Posts: 7
Location: Germany, Munich
Robert Reiner
Offline
Registered User
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?
Reply With Quote

#2 Old 2004-04-25, 09:28

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
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
Reply With Quote

#3 Old 2004-04-25, 16:53

Join Date: Apr 2004
Posts: 7
Location: Germany, Munich
Robert Reiner
Offline
Registered User
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.
Reply With Quote

#4 Old 2004-04-25, 17:07

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
OK. So what do you do when you say:
Code:
// do something
shmoove
Reply With Quote

#5 Old 2004-04-25, 19:38

Join Date: Apr 2004
Posts: 7
Location: Germany, Munich
Robert Reiner
Offline
Registered User
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.
Reply With Quote

#6 Old 2004-04-26, 11:27

Join Date: Jul 2003
Posts: 1,094
Location: Finland, Tampere
doctordwarf
Offline
Super Contributor
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
Reply With Quote

#7 Old 2004-04-26, 11:46

Join Date: Apr 2004
Posts: 7
Location: Germany, Munich
Robert Reiner
Offline
Registered User
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...
Reply With Quote

#8 Old 2004-04-26, 12:08

Join Date: Jul 2003
Posts: 1,094
Location: Finland, Tampere
doctordwarf
Offline
Super Contributor
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
Reply With Quote

#9 Old 2004-04-26, 13:27

Join Date: Apr 2004
Posts: 7
Location: Germany, Munich
Robert Reiner
Offline
Registered User
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).
Reply With Quote

#10 Old 2004-04-26, 14:15

Join Date: Jul 2003
Posts: 1,094
Location: Finland, Tampere
doctordwarf
Offline
Super Contributor
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
Reply With Quote

#11 Old 2004-04-26, 14:17

Join Date: Jul 2003
Posts: 1,094
Location: Finland, Tampere
doctordwarf
Offline
Super Contributor
Hey!

What does the following mean?!
Code:
catch (final Exception e)
{
I've never seen such code.. in fact I was even unaware, that such a syntax is possible :)
Can it be so, that it prevents the phone from handling your Exception? Try usual syntax:
Code:
catch (Exception e)
Reply With Quote

#12 Old 2004-04-26, 23:18

Join Date: Apr 2004
Posts: 7
Location: Germany, Munich
Robert Reiner
Offline
Registered User
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. :-(
Reply With Quote

#13 Old 2004-04-27, 09:17

Join Date: Jul 2003
Posts: 1,094
Location: Finland, Tampere
doctordwarf
Offline
Super Contributor
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 With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia