Java JNI call to load library

14,945

Solution 1

No, it doesn't matter. It's harmless to call loadLibrary() more than once in the same classloader.

From the documentation for Runtime.loadLibrary(String), which is called by System.loadLibrary(String):

   If this method is called more than once with the same library name, 
   the second and subsequent calls are ignored.

Solution 2

Its better to have the class which uses the library, load the library. If you have to caller load the library you make it possible to call the native methods without loading the library.

Solution 3

Jni libs are dynamic libs. I'd think they'd have to be in order to be loaded by loadLibrary. One of the advantages of dynamic libraries is that if they are already loaded into memory, that copy gets used instead of being reloaded. So you can use the two loadlibrary calls.

The other issue is that if you put the loadlibrary call in class C, you've ruined the encapsulation of the other two classes. In any large project, someone is eventually going to call one of the native calls in class a or class b without going through class c. That will not work so well.

Share:
14,945
JPM
Author by

JPM

Java Programmer for 6 years and seasoned Android developer, some C#, slightly new Windows Mobile developer. Member of the Bluetooth SIG Naming and Version Committee Musically inclined, play guitar and singer/songwriter, band investor among many other titles in the music biz. I am nerdier than 95% of all people. Are you a nerd? Click here to take the Nerd Test, get nerdy images and jokes, and talk on the nerd forum! http://www.nerdtests.com/images/ft/nq/a287f5ee0b.gif

Updated on July 19, 2022

Comments

  • JPM
    JPM almost 2 years

    Does it impact memory if I have two Java classes that have native calls to compiled C code and I call both those classes in another class? For instance I have Class A and Class B with both calls to native functions. They are setup like this:

    public class A{
        // declare the native code function - must match ndkfoo.c
        static {
            System.loadLibrary("ndkfoo");
        }
    
        private static native double mathMethod();
    
        public A() {}
    
        public double getMath() {
              double dResult = 0;  
              dResult = mathMethod();
              return dResult;
        }
    }
    
    
    public class B{
        // declare the native code function - must match ndkfoo.c
        static {
            System.loadLibrary("ndkfoo");
        }
    
        private static native double nonMathMethod();
    
        public B() {}
    
        public double getNonMath() {
              double dResult = 0;  
              dResult = nonMathMethod();
              return dResult;
        }
    }
    

    Class C then calls both, since they both make a static call to load the library will that matter in class C? Or is it better to have Class C call System.loadLibrary(...?

    public class C{
        // declare the native code function - must match ndkfoo.c
        //  So is it beter to declare loadLibrary here than in each individual class?
        //static {
        //  System.loadLibrary("ndkfoo");
        //}
        //
    
        public C() {}
    
        public static void main(String[] args) {
            A a = new A();
            B b = new B();
            double result = a.getMath() + b.getNonMath();
    
        }
    }