0 Replies Latest reply: Aug 21, 2009 8:34 AM by 843851 RSS

    JNI in Xlet, is it possible?

    843851
      I'm trying to make bindings in a 'normal' Xlet to JNI to c++. However when I try to load a native library (libstringcpp.so) as static in the Xlet class I get some exceptions. Those exceptions appears to be related to AccessController or the java security policy. I tried googling for that but didn't succeed. If you guys know the solution or can point me to a solution please say so ;)

      Heres the XletManager code
      import java.io.IOException;
      import com.sun.xlet.XletLifecycleHandler;
      import com.sun.xlet.XletManager;
      
      public class Main {
      
          public static void main(String args[]) throws InterruptedException {
              XletLifecycleHandler appJNI = null;
      
              // Creating Xlet
              try {
                  String classpath = System.getProperty("java.class.path", ".");
                  appJNI = XletManager.createXlet("JNIStr");
                  System.out.println("JNI Createdn\n");
              } catch (IOException ex) {
                  ex.printStackTrace();
                  System.exit(1);
              }
      
              System.out.println("PostINIT()");
              appJNI.postInitXlet();
      
              System.out.println("PostSTART()");
              appJNI.postStartXlet();
      
              Thread.sleep(5000);
      
              appJNI.postDestroyXlet(true);
      
              System.out.println("Manager dying...");
          }
      }
      Heres the Xlet code.
      import javax.microedition.xlet.Xlet;
      import javax.microedition.xlet.XletContext;
      import javax.microedition.xlet.XletStateChangeException;
      import javax.microedition.xlet.ixc.IxcRegistry;
      import java.security.*;
      
      public class JNIStr implements JNIProxy, Xlet {
      
          // Native library
          static {
              System.loadLibrary("stringcpp");
          }
      
          private XletContext context;
      
          public void startXlet() throws XletStateChangeException {
              try {
                  setString("String from Xlet initialization");
      
                  // JNI Tests
                  System.out.println("Java calling method getString(): " + getString() + "\n\n");
      
                  // Running setString();
                  setString("JNI Works");
                  System.out.println("Java calling setString("JNI Works!\")\n\n");
      
                  // Verifying if setString() worked
                  System.out.println("Java calling getString(): " + getString() + "\n\n");
      
              } catch (Exception e) {
                  System.out.println("Error");
              }
          }
      
          public void destroyXlet(boolean unconditional) throws XletStateChangeException {
              System.out.println("Destroying JNI\n");
          }
      
          public void initXlet(XletContext ctx) throws XletStateChangeException {
              this.context = ctx;
              System.out.println("JNIStr init");
          }
      
          public void pauseXlet() {}
      
          // JNI Methods
          public native String getString();
          public native void setString(String str);
      }
      ufg@ufg-laptop:~/NetBeansProjects/JNIJava/build/classes$ phoneME -Djava.library.path='/home/ufg/NetBeansProjects/JNIJava/build/classes' Main
      java.lang.reflect.InvocationTargetException
           at com.sun.xlet.XletManager.handleRequest(Unknown Source)
           at com.sun.xlet.XletStateQueue.dispatchRequests(Unknown Source)
           at com.sun.xlet.XletStateQueue$1.run(Unknown Source)
           at java.lang.Thread.run(Unknown Source)
           at java.lang.Thread.startup(Unknown Source)
      Caused by: java.lang.ExceptionInInitializerError
           at java.lang.Class.runStaticInitializers(Unknown Source)
           ... 5 more
      *Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission loadLibrary.stringcpp)*
           at java.security.AccessControlContext.checkPermission(Unknown Source)
           at java.security.AccessController.checkPermission(Unknown Source)
           at java.lang.SecurityManager.checkPermission(Unknown Source)
           at java.lang.SecurityManager.checkLink(Unknown Source)
           at java.lang.Runtime.loadLibrary0(Unknown Source)
           at java.lang.System.loadLibrary(Unknown Source)
           at JNIStr.<clinit>(Unknown Source)
           ... 6 more
      java.lang.NullPointerException
           at com.sun.xlet.XletManager.handleRequest(Unknown Source)
           at com.sun.xlet.XletManager.handleRequest(Unknown Source)
           at com.sun.xlet.XletStateQueue.dispatchRequests(Unknown Source)
           at com.sun.xlet.XletStateQueue$1.run(Unknown Source)
           at java.lang.Thread.run(Unknown Source)
           at java.lang.Thread.startup(Unknown Source)Aplicação JNI criada
      
      
      PostINIT()
      
      PostSTART()