Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I avoid calling System.load twice?
    text
    copied!<p>I have a class that calls a native function to get information about a system from its CMOS. The class has a static initialization block which loads the library containing the native function, and it looks something like this:</p> <pre><code>package lib.sysid; public class SysId { private static native int getSysIdNative(); private static final String SYS_ID_PATH = "libsysid.so"; static { System.load(SYS_ID_PATH); } public static int getSysIdFromCMOS() { int returnValue = getSysIdNative(); } } </code></pre> <p>According to my testing, the method works fine the first time I use it, but if I call the method again at a later time, the static initialization block also runs, causing an UnsatisfiedLinkError:</p> <pre><code>java.lang.UnsatisfiedLinkError: Native Library libsysid.so already loaded in another classloader </code></pre> <p>How can I avoid the static initialization block from executing the <code>System.load()</code> method if it has already been run?</p> <p>Alternatively, is there a way for me to attempt to "unload" the library if it is already loaded before calling the <code>System.load()</code> method again?</p> <p>EDIT: Strangely enough, if I surround the <code>System.load()</code> call with a try-catch block, I still get an UnsatisfiedLinkError, but this time it comes from the actual call to <code>getSysIdNative()</code>. The error I see is the following: </p> <pre><code>lib.sysid.SysId.getSysIdNative()I </code></pre> <p>What the heck is that "I" that shows up? I've tried to attach a debugger to this code to see where the message gets populated, but so far I haven't been successful.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload