×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Regular Contributor
    Join Date
    Dec 2003
    Posts
    109

    Multiple Classes with WTK

    I am trying to make a simple application, made up of two classes with Wireless Tool Kit.

    The problem is this. I have not been able to figure out how to initialize one class from within another. With J2SE, I would simply include package reference to all classes within the same package, but it does not seem to work in this case - I get "java.lang.ClassNotFoundException" instead.

    If I am not using the package reference, I get the following message:
    java.lang.SecurityException: Application not authorized to access the restricted API

    at com.sun.midp.security.SecurityToken.checkIfPermissionAllowed(+40)

    at com.sun.midp.security.SecurityToken.checkIfPermissionAllowed(+7)

    at com.sun.midp.midletsuite.MIDletSuiteImpl.checkIfPermissionAllowed(+8)

    at com.sun.midp.midlet.MIDletState.<init>(+66)

    at javax.microedition.midlet.MIDletProxy.<init>(+5)

    at javax.microedition.midlet.MIDlet.<init>(+13)

    at MakeHttpConnection.<init>(+4)

    at javaMail.commandAction(+15)

    at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+284)

    at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)

    at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)

    at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)

    at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+250)


    I am a total novice to java and mobile java, in particular. For anyone with some experience in J2ME the answer should be obvious. I hope you can share some thoughts with me. Thank you.

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    If both classes are in the same package then you don't need to import anything, just refer to the class by it's name:
    mypackage/ClassOne.java -
    Code:
    package mypackage;
    
    public class ClassOne {
    // ...
    }
    mypackage/ClassTwo.java -
    Code:
    package mypackage;
    
    public class ClassTwo {
    // ...
      void someMethod() {
        ClassOne x = new ClassOne();
      }
    }
    If the classes are in different classes then you have to import the package in the class that uses the other one:
    mypackage/ClassOne.java -
    Code:
    package mypackage;
    
    public class ClassOne {
    // ...
    }
    myotherpackage/ClassTwo.java -
    Code:
    package myotherpackage;
    
    import mypackage.*; // import all the classes from mypackage
    // you can use : 
    // import mypackage.ClassOne; 
    // to import just that class
    
    public class ClassTwo {
    // ...
      void someMethod() {
        ClassOne x = new ClassOne();
      }
    }
    And using a fully qualified name (packagename.classname) should always work:
    mypackage/ClassOne.java -
    Code:
    package mypackage;
    
    public class ClassOne {
    // ...
    }
    myotherpackage/ClassTwo.java -
    Code:
    package myotherpackage;
    
    public class ClassTwo {
    // ...
      void someMethod() {
        mypackage.ClassOne x = new mypackage.ClassOne();
      }
    }
    shmoove

  3. #3
    Regular Contributor
    Join Date
    Dec 2003
    Posts
    109
    Thank you for the post. Both classes ARE within the same package. The problem is that when I try to run the MIDlet I keep getting the same error:

    Unable to create MIDlet ClassName

    java.lang.ClassNotFoundException: ClassName

    at com.sun.midp.midlet.MIDletState.createMIDlet(+14)

    at com.sun.midp.midlet.Selector.run(+22)

    Execution completed.


    If it matters, I am using the Wireless Toolkit 2.1 ... any ideas of what I'm doing wrong? Thanks again.

  4. #4
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Is there a class called ClassName that extends MIDlet in your application? Is it in the root package? If not then you should reflect that in the MIDlet-1 property of the jad.
    For example, if you have ClassName in a package called packagename then the line in the jad should be something like:
    Code:
    MIDlet-1: MyMIDlet, , packagename.ClassName
    and not:
    Code:
    MIDlet-1: MyMIDlet, , ClassName
    shmoove

  5. #5
    Regular Contributor
    Join Date
    Dec 2003
    Posts
    109
    Yes, the JAD generated by the WTK does specify package.ClassName but I still get error messages. Perhaps, and quite likely, I am missing the basic idea of MIDlet suite. Here is what I am trying to accomplish:

    I have one class gather user information - FirstClass. The second class sends that information via HTTP - SecondClass.

    At the start-up screen of a MIDlet suite I want to have only one menu item - the FirstClass (user should not be able to invoke SecondClass directly).

    So this is what I did:
    1. the first line in both classes is "package mail;"
    2. FirstClass with commandAction initiates class two: new SecondClass();
    3. the project specifies only one MIDlet - pointing to "mail.FirstClass";

    When done this way, when I implement commandAction I recieve a long error message starting with "Application not authorized to access the restricted API".

    As I said, perhaps, I misunderstand the concept of a MIDlet. At this time, I am trying to do things similarly to J2SE - I have one class with a main method (StartApp() in J2ME), and other classes in the same package are initialized from that class. Is that not the case with MIDlets. Thanks again. Your help is very appreciated.

  6. #6
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Your FirstClass should inherit from MIDlet and if you are implementing the commandAction there then it should implement CommandListener:
    Code:
    package mail;
    
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    public class FirstClass extends MIDlet implements CommandListener {
      // ....
    }
    And if SecondClass is in the same package then you don't need any imports to use it from first class.

    But I have a feeling that you're just packaging the application wrong. If the classes are in a package called mail, then they also have to be in a subdirectory of the same name:
    [source code root dir]\mail\FirstClass.java
    [source code root dir]\mail\SecondClass.java
    And the classes would be compiled into:
    [classpath dir]\mail\FirstClass.class
    [classpath dir]\mail\SecondClass.class

    This same structure has to be kept in the jar as well.

    If they're not there the VM won't be able to find them.

    shmoove

  7. #7
    Regular Contributor
    Join Date
    Dec 2003
    Posts
    109
    Thank you very much for your help. Issue has been resolved - your last comment pointed in the right direction.

    My SecondClass had pauseApp and destroyApp methods that apparently need only be in the main class of a MIDlet. Must be obvious to anyone, but a beginner as myself.

    Thank you once again.

Posting Permissions

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