×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Regular Contributor
    Join Date
    Mar 2009
    Posts
    80

    java.lang.NoClassDefFoundError

    Old code, again.
    Problem:
    I use the Sensor framework to check the Battery level for certain terminals that do not use the System.getProperty("...batterylevel") method. So I have the Sensor packages included in my MIDlet, see below. This code has run fine for 7 months on my Nokia 6210 as on other terminals. Of cource I check in the checkBattery() method wich Manufacturer we are running inside and use the correct method calls accordingly.

    However, as of today, after having fixed yeasterdays different challeges, this new error message pops up directly when I start the application in my Nokia 6210:

    Unexpected exception
    java.lang.NoClassDefFoundError
    javax.microedition.sensor.SensorConnection

    After this the app exits.

    Questions:
    1) Is this error message caused the imported packages, i.e.:
    import javax.microedition.sensor.*;
    import javax.microedition.sensor.SensorConnection;
    import javax.microedition.sensor.SensorInfo;
    import javax.microedition.sensor.SensorManager;
    import javax.microedition.sensor.MeasurementRange;
    import javax.microedition.sensor.ChannelInfo;

    2) Or, is this error message caused by the application (in an errounous way) somewhere tries to execute the SensorConnection method not availble in the Nokia 6210 Nav?

    3) If none of above, then whatcan cause this error message?

    thanx in advance
    /Per

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

    Re: java.lang.NoClassDefFoundError

    "import" statements are not an issue, as they do not generate any code.

    NoClassDefFoundError occurs for one of three reasons. The VM is attempting to resolve a reference to a class, but:

    1. no ".class" file matching the class's name can be found;

    2. a ".class" file can be found, but it does not contain a definition for the named class; or

    3. a previous attempt to resolve the same class name failed due to an exception being thrown in the class's static initializer.

    In your case, (1) is the problem, because the classes to which your code is referring are not present.

    Class resolution occurs when:

    1. there has been a call to Class.forName()

    2. a class is referenced from another class

    In the case of (2), for example, where сlass A contains a reference to class B, resolution occurs after class A is loaded and verified, and before any code in class B needs to be executed. How long before code in B needs to be executed depends on the VM implementation. Resolution of B may occur immediately after A is loaded, or it may be deferred until B is instantiated or a static member is accessed.

    Because of this difference in when resolution may occur, it can be enough on some devices simply to avoid executing any code that references the class. However, this will fail on devices that attempt name resolution earlier.

    This document in the wiki contains a more detailed explaination of the loading process, and a technique for using classes that might not be present at run-time.

    Graham.

  3. #3
    Regular Contributor
    Join Date
    Mar 2009
    Posts
    80

    Re: java.lang.NoClassDefFoundError

    Thanx Graham,
    Am I understanding you correctly that this Exception is caused by my app calls a SensorConnection class in the Nokia 6210 Nav?
    This means I have an error i the code, since inside a Nokia 6210 sensors are not used.

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

    Re: java.lang.NoClassDefFoundError

    No.

    It is not necessary to call a method in the sensor API classes for this to happen. On some devices, it may happen simply because you have code in a loaded class that access the sensor API even though you will never execute this code.

    For example:

    Code:
    boolean hasSensorAPI = System.getProperty("microedition.sensor.version") != null;
    
    if (hasSensorAPI) {
        SensorInfo[] info = SensorManager.findSensors("sensor:temperature");
    }
    On some devices, this can throw a NoClassDefFoundError, due to an attempt to resolve the class name "SensorInfo", even though the code will not be executed unless that API exists.

    Graham.

  5. #5
    Regular Contributor
    Join Date
    Mar 2009
    Posts
    80

    Re: java.lang.NoClassDefFoundError

    Good answers, thanks.

    Since this code worked on the Nokia 6210 Nav for a long time, something changed recently.
    Can the SDK have played a role here?

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

    Re: java.lang.NoClassDefFoundError

    No. Have you changed your code?

  7. #7
    Regular Contributor
    Join Date
    Mar 2009
    Posts
    80

    Re: java.lang.NoClassDefFoundError

    As a matter of fact, yes.
    The code to get the Battery charging was involved in the NullPointerException in the Runnable of a Canvas class that we discussed yesterday. So, you are probably on the right track. Here's the code involved:

    MIDlet:
    *******
    .
    .
    // Service method for the GUI classes
    public int BatteryUpdateManager(){
    if(Manufacturer != null && Manufacturer.length() > 1 ){ <--- new check
    // *** NOKIA ***
    if ( Manufacturer.indexOf("Nokia") > -1){
    Manufacturer = "Nokia";
    batteryStatusStr = System.getProperty("com.nokia.mid.batterylevel");
    if (batteryStatusStr != null) {
    batteryStatusInt = Integer.parseInt(batteryStatusStr);
    } else {
    // Battery string = null
    batteryStatusInt = -1;
    }
    // *** SonyEricsson ***
    } else if (Manufacturer.indexOf("SonyEricsson") > -1){
    Manufacturer ="SonyEricsson";
    // Check the Sensor support
    String sensorVersion = System.getProperty("microedition.sensor.version");
    if (sensorVersion != null) {
    SensorInfo[] batteryInfos = SensorManager.findSensors("battery_charge", null);
    try{
    SensorConnection sc = (SensorConnection)Connector.open(batteryInfos[0].getUrl());
    Data data[] = sc.getData(1);
    batteryStatusInt = data[0].getIntValues()[0];
    if (batteryStatusInt == 0) {
    batteryStatusInt = -1;
    }
    } catch (Exception ex) {
    alertUser = new Alert("SEbattConnOpen", ex.getMessage() ,null,null);
    alertUser.setTimeout(5000);
    alertUser.setType(ERROR);
    Display.getDisplay(this).setCurrent(alertUser);
    }
    } // SonyEricsson terminal - not supporting Sensors
    else {
    batteryStatusInt = -1;
    }
    }
    //**** Unknown Manufacturer:
    else {
    batteryStatusInt = -1;
    }

    } else{ <------------ else part of new check
    // Manufacturer string null or empty
    batteryStatusInt = -1;
    }
    return batteryStatusInt;
    }

    CANVAS CLASS RUNNABLE
    *********************
    public void run() {
    while (true) {
    try{ <-------------------- New try block
    /*
    * Check the battery status
    */
    batteryStatus = midlet.BatteryUpdateManager(); // int
    if(batteryStatus != -1){
    batteryStatusF = (float)batteryStatus;
    .
    nothing changed
    .


    } catch (Exception ex){ <------- New CATCH/alertUser block
    alertUser = new Alert("21. SafeMap Runnable error", ex.toString() ,null,null);
    alertUser.setTimeout(3000);
    alertUser.setType(AlertType.ERROR);
    Display.getDisplay(midlet).setCurrent(alertUser);
    }

    repaint();
    // Main loop
    try {
    Thread.sleep(15000);
    }
    catch (Exception ex) {
    alertUser = new Alert("22. SafeMap Runnable Sleep Error",ex.toString(),null,null);
    alertUser.setTimeout(3000);
    alertUser.setType(AlertType.ERROR);
    Display.getDisplay(midlet).setCurrent(alertUser);
    }

    }

    } // while
    } // Run

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

    Re: java.lang.NoClassDefFoundError

    The code was always going to exhibit this problem on some device sooner or later. Something you've changed made it appear on this device. Basically, you mustn't allow any class to load if it contains a reference to an API that doesn't exist.

    Oh, and...

    Code:
    while (true) {
    ...is never a good idea. Have a boolean to control the exit of the loop (and so, to allow the thread to die), and make sure it gets set - say, in the destroyApp() event.

  9. #9
    Regular Contributor
    Join Date
    Mar 2009
    Posts
    80

    Re: java.lang.NoClassDefFoundError

    Hi Graham,
    Fixed. Thank's for the valuable background information.
    /Per

Similar Threads

  1. ERROR: java.lang.NoClassDefFoundError: org/kxml/parser/XmlParser
    By dhiraj.deka in forum Mobile Java General
    Replies: 9
    Last Post: 2009-11-07, 09:34
  2. java.lang.NoClassDefFoundError with JSR75
    By erracid8 in forum Mobile Service Architecture (Closed)
    Replies: 6
    Last Post: 2009-08-28, 13:17
  3. really annoying java.lang.NoClassDefFoundError:
    By TacB0sS in forum Mobile Java General
    Replies: 2
    Last Post: 2009-06-10, 02:20
  4. java.lang.NoClassDefFoundError - javax.obex.Operation
    By lucianblue in forum Mobile Java Networking & Messaging & Security
    Replies: 6
    Last Post: 2007-06-30, 15:04
  5. Java.lang.NoClassDefFoundError !!?? WTF
    By Woody_FX in forum Mobile Java General
    Replies: 1
    Last Post: 2004-01-24, 13:32

Posting Permissions

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