×

Discussion Board

Results 1 to 15 of 15
  1. #1
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Cool Load class dynamically in J2ME

    Hi friends,

    I'm in desperate need of help. I need to load a class dynamically during runtime. The class is in a jar file in the resources. Could anyone help me please ?? There's no other way to this solution because i need to load a class that i made to use methods that i defined in case the phone supports gps.

    So the logic will be:

    if (gpsSupported){
    loadclass();
    usemethod();
    }

    Anyway can help me please ??? At least maybe there's another way around this problem i have...

  2. #2
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    88

    Re: Load class dynamically in J2ME

    Hello,
    you can use class.forName() method. However, why are u keeping the class into the resource file!?!

    []'s

  3. #3
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Re: Load class dynamically in J2ME

    Hi friend,

    I know that there was something that could be done with the class.forName() but i'm not sure how to do it exactly.

    I'm keeping them in the resources folder, because i need to share the classes with other projects. So i've created a project with the classes and i'm sharing the jar file with the projects where i need to use the classes.

    I'm working this way because the jar will be updated automatically whenever i re-build the project with the classes.

    I'm a bit new to such things in J2ME so any comments will be highly appreciated

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    42

    Re: Load class dynamically in J2ME

    I needed to do the exact same thing. This linked helped me a lot and maybe it will help you as well

    http://wiki.forum.nokia.com/index.ph...API_in_Java_ME

  5. #5
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Smile Re: Load class dynamically in J2ME

    Hi,

    Thanks a lot mate, i will give it a better look but it seems that it is going to solve my problem

    Regards,
    Kurt

  6. #6
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Cool Re: Load class dynamically in J2ME

    By the way...

    Because this works for half of my problem... (gps - location)

    How about the nfc ?? Because in nfc, i need to register a targetlistener... and and when a target is detected there's a method: targetDetected();

    That is not my actual problem, my actual problem is that from that method i need to access a method from the mainclass i have (from the application) and i can't get the method sendBeep() from my main class, and it's not clean coding to have double methods... :-/

  7. #7
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Load class dynamically in J2ME

    The same technique works for any API.

    Graham.

  8. #8
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Re: Load class dynamically in J2ME

    Yes i read that part, but i'm not sure how to code it.

    When i'll specify the target listener, which is going to be the target listener ? The class that i will create ?

    And when a target will be detected, how am i going to sort of notify my main application class ? because that's my main problem with using it with nfc, since it needs the targetlistener...

  9. #9
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Load class dynamically in J2ME

    The technique effectively involves constructing an abstraction layer between your application and the API. If you need to implement API-specific interfaces (like LocationListener, TargetListener, PlayerListener, etc.), they must be implemented in the non-public classes of the abstraction layer. If you need to deliver that information back to the application in the same way, then you may need to provide your own "listener" interface as part of your abstration layer, but make sure it doesn't use classes that are part of the optional API.

    Graham.

  10. #10
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Re: Load class dynamically in J2ME

    Mmm will try to work on it will i will post some updates in the near future... thanks mate

  11. #11
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Unhappy Re: Load class dynamically in J2ME

    Quote Originally Posted by NightStalkerDNS View Post
    I needed to do the exact same thing. This linked helped me a lot and maybe it will help you as well

    http://wiki.forum.nokia.com/index.ph...API_in_Java_ME
    Hi thanks a lot for the link it helped me, but not i'm getting confused. Because i've got two LocationProvider classes... one was provided by the SDK and the one i created now.

    My old code was:

    String data = "";
    // Set the criteria for selecting a location provider
    cr = new Criteria();
    cr.setHorizontalAccuracy(horizontalAccuracy);
    // Get an instance of the location provider
    LocationProvider lp = LocationProvider.getInstance(cr);
    // Request the location from the location provider
    Location l = lp.getLocation(locationTimeout); //60 seconds timeout
    Coordinates c = l.getQualifiedCoordinates();
    if (c != null) {
    // Send coordinate information
    data = new String("GPSFIX " + dateToString(now()) + " " + c.getLatitude() + " " + c.getLongitude());
    }
    return data;

    (Because currently i'm sending text to the server with the time, latitude and longitude)

    But still i'm not sure how to work with these new classes because i've got the method .getLongitude .getLatitude to implement in the LocationImplementation class which i don't know how am i going to implement.
    My actual problem is how to get the location, because my LocationProvider class is just a wrapper class, while before i was using it to get the location... :-/

    Any help will be highly appreciated, thanks friends and happy new year to everyone

  12. #12
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Load class dynamically in J2ME

    The code to access the Location API (and so to use classes like Criteria, Location, Coordinates, etc.) must all be in the class that implements LocationProvider. Otherwise, the device will attempt to link the classes when it doesn't have them (and execution will fail). You must not import location API classes outside that LocationProvider implementation.

    The LocationProvider object needs to call getLocation() (or, better still, use a LocationListener) to get the information you want. As I wrote before, you would be better to implement a "listener" model to get the information asynchronously. Methods of the LocationProvider must not return Location API objects.

    That code in the wiki is an example of how to detect APIs at run-time, and use them only if present. It is a generic technique, and is not specifically about using the location API (that's just used as an example). The specific methods (like getLat(), getLong()) are given just for the sake of a simple example, and are not necessarily the best way to use the location API.

    Graham.

  13. #13
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Exclamation Re: Load class dynamically in J2ME

    Hi,

    Thanks a lot for your help mate, i worked it out very similar to the example and i suceeded sucessfully because i got the concept pretty well.

    The only problem i have is to implement a listener interface. You told me that i could create a listener model. Could you maybe explain a bit more please. I know what a listener is and it's function but i don't know how to implement the listener to the location provider implementation.

    Actually my other part is nfc, so i need to implement targetListener to the contactlessImplementation but when i'm adding the small piece of code: implements TargetListener it isn't deploying sucessfully and it's giving me the error:

    error preverifying class TargetListener :-/

    Any help will be highly appreciated, thanks !!

  14. #14
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Load class dynamically in J2ME

    A listener is easy enough. You need an interface (I'll continue the form of the example code in the wiki):

    Code:
    package com.mycompany.locationservices;
    
    public interface LocationListener {
        // use separate arguments, because we can't pass a Location object to the main app.
        public void newLocation(double lat, double long, float altitude, float horizAcc, float vertAcc);
    }
    Modify the abstract class:

    Code:
    public abstract class LocationProvider {
        // this class doesn't import the Location API, so this is our own listener
        private LocationListener listener;
        public void setLocationListener(LocationListener l) {
            listener = l;
        }
        protected void notifyListener(double lat, double long, float altitude, float horizAcc, float vertAcc) {
            if (listener != null) {
                listener.newLocation(lat, long, altitude, horizAcc, vertAcc);
            }
        }
    }
    In the implementation class, implement the Location API's LocationListener, and when you receive an event, get the location information and pass it to notifyListener(), which will then forward the details to the rest of the application for you.

    We have to create a new listener interface, because we can't use the Location API's interface outside of the location implementation package. If we did use the Location API's interface in the main application code, we'd cause the device to look for the Location API, and fail on devices that don't have it (same for any other API).

    Similarly, which the Location API's interface specifies a QualifiedLocation object as the argument to the listener method, we have to use separate arguments (or create our own "location" class). Again, if the main application code contains a reference to a Location API interface or class, it may fail on devices with no Location API.

    You will need the Location API (or NFC, or whatever) in the class path when building, or the classes that refer to those APIs won't compile or preverify. This won't cause a problem when running on devices without that API, provided you stick to the rules I've described.

    Graham.

  15. #15
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Exclamation Re: Load class dynamically in J2ME

    Hi,

    I've implemented everything basically i had some things a bit different but with the same thoughts in mind but still i'm having the preverifying error.

    Quote Originally Posted by grahamhughes View Post
    You will need the Location API (or NFC, or whatever) in the class path when building, or the classes that refer to those APIs won't compile or preverify. This won't cause a problem when running on devices without that API, provided you stick to the rules I've described.
    When you said that i need the nfc API (because now i'm working with nfc) in the class path when building. The fact that i'm working in a different project and putting the distributable jar in the resources, that should make my classes in the class path right ? i mean how could i make sure that the nfc API will be in the class path when building (because actually i'm using the nfc api in the other project where i'm building the contactless services)

    Btw i'm not using javax packages anymore so that i can be sure that i'm not having problems because the package is javax.

    Btw thanks a lot for your help, i feel that i'm veryy close to the solution because again, the only time i'm having a preverifying error is when im implementing the TargetListener in the contactlessImplementation class... else it isn't giving me any errors...

Similar Threads

  1. DateField class of J2ME
    By yasomca in forum Mobile Java Tools & SDKs
    Replies: 9
    Last Post: 2010-09-06, 10:50
  2. Error loading connection when obfuscation high?
    By ejakkk in forum Mobile Java General
    Replies: 8
    Last Post: 2009-06-26, 12:54
  3. External Linking Error (Undefined Symbol)
    By nile_mail in forum Symbian
    Replies: 2
    Last Post: 2008-08-05, 13:10
  4. Need one HTTP client example
    By vinayakak in forum Symbian Networking & Messaging (Closed)
    Replies: 15
    Last Post: 2008-01-06, 06:44
  5. Where does the class load in Memory and How much Memory Consume
    By sharma_durg in forum Mobile Java General
    Replies: 2
    Last Post: 2006-10-13, 22:34

Posting Permissions

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