Announcement Announcement Module
Collapse
No announcement yet.
problem with native code Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • problem with native code

    I depend on a library that has native code. I created a bundle from that library, but it's not working as expected.

    The native code calls
    _env->FindClass("name/of/class")
    where the class is the same class that contains references to native methods

    the function returns NULL even though it's the same class that causes the native methods to be invoked.
    I'm guessing it's something to do with the way classloading works? I know little about JNI. Any ideas on how to address this?

  • #2
    problem with native code

    Vadim,

    FindClass(JNIEnv*, char*) looks only on the configured classpath for classes (http://java.sun.com/j2se/1.4.2/docs/guide/jni/spec/functions.html#wp16027).

    I think the best option is to pass in the class into your native code. If this is not possible, you'll need to locate the correct ClassLoader in native code and then invoke loadClass() on it.

    Rob

    Comment


    • #3
      problem with native code

      Ah that explains it. I guess I'll have to modify the library. Thanks.

      Comment


      • #4
        problem with native code

        Thanks, I modified the native code to pass a reference to the original class and that works, but there's a strange issue with logging.

        The native code has callbacks that end up firing listeners in Java. When using a logger (slf4j), events don't end up in the bundle's trace file. It does work outside the listener.

        It's something like this:

        public class ServiceImpl {
        private Logger logger = LoggerFactory.getLogger(ServiceImpl.class);

        private class ListenerImpl implements Listener {
        void somethingHappened() {
        logger.info("Something Happened");
        }
        }

        private void doSomething() {
        logger.info("Starting native app");
        NativeApp app = new NativeApp(new ListenerImpl());
        app.start();
        }
        }


        The log statement in doSomething will show up in the bundle's trace file, but the listener log statement wont.
        They both show up in the global trace file.

        Comment


        • #5
          problem with native code

          And another weird thing...

          logger.info("{} {} {}", new Object[] { "aaa", "bbb", "ccc"} );

          Outside of spring dm server, this outputs
          aaa bbb ccc

          Inside dm server, this just outputs
          [Ljava.lang.Object;@158ae49 {} {}

          Comment

          Working...
          X