3 Replies Latest reply: May 11, 2010 2:05 AM by user2099262 RSS

    Loading system libraries from java at runtime.

      I was wondering how I can load required system libraries at runtime. I have BDB XML and use java swing for querying and putting documents. I know that I can use -Djava.library.path (and BTW works fine), but I want to do in runtime since that installation directory may be different form user to user. Right now, I am testing on my mac with the following code:
              File libFolder = new File("/opt/dbxml-2.5.16/lib/");
              File[] libs=libFolder.listFiles(new FilenameFilter(){
                  public boolean accept(File file, String s) {
                      return s.endsWith("dylib");
              for (File lib : libs) {
              EnvironmentConfig envConf = new EnvironmentConfig();
              try {
                  new Environment(new File("/var/dbxml"), envConf);
              } catch (Exception e) {
                  throw new RuntimeException(e);
      But that does not seem to work. I keep getting "Exception in thread "main" java.lang.UnsatisfiedLinkError: no db_java-4.8 in java.library.path".

      Any tips appreciated.
        • 1. Re: Loading system libraries from java at runtime.

          the cause of a problem is that DB XML Java classes (namely, db_javaJNI) load DB XML C++ libraries with a code line:
          System.loadLibrary("db_java-" +  v_major + "." + v_minor);
          that is the DB XML C++ libraries should be situated in java.library.path anyway. That's where you get an exception.
          To dynamically overwrite the java.library.path system variable you could use the hack described for example in:

          • 2. Re: Loading system libraries from java at runtime.
            I encountered same problem before.
            It seems dbxml-java will load the approprate libraries it self.
            So load it explicitly in your java application does not help,

            It is better to try to configure correct library path and let dbxml-java found it at runtime by it self.
            • 3. Re: Loading system libraries from java at runtime.
              This is how I fixed the problem (ref. Vyacheslav's link). Changing sys-prop java.library.path won't work at runtime, cause it is read-only.
              public static void setJavaLibPath(String classpath)  {
                              System.out.println("setting java.library.path to "+classpath);
                        try {
                             Field field = ClassLoader.class.getDeclaredField("usr_paths");
                             String[] paths = (String[])field.get(null);
                                      for (String path : paths) {
                                          if (classpath.equals(path))
                             String[] tmp = new String[paths.length+1];
                             tmp[paths.length] = classpath;
                        } catch (IllegalAccessException e) {
                        } catch (NoSuchFieldException e) {
              Edited by: user2099262 on May 11, 2010 12:03 AM

              Edited by: user2099262 on May 11, 2010 12:04 AM