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

    Loading system libraries from java at runtime.

    user2099262
      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(){
                  @Override
                  public boolean accept(File file, String s) {
                      return s.endsWith("dylib");
                  }
              });
              for (File lib : libs) {
                  System.out.println("loading..."+lib.getAbsolutePath());
                  System.load(lib.getAbsolutePath());
              }
      
              EnvironmentConfig envConf = new EnvironmentConfig();
              envConf.setAllowCreate(true);
              envConf.setInitializeCache(true);
              envConf.setInitializeLocking(true);
              envConf.setInitializeLogging(true);
              try {
                  new Environment(new File("/var/dbxml"), envConf);
              } catch (Exception e) {
                  e.printStackTrace();
                  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.
          637288
          Hi,

          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:
          http://forums.sun.com/thread.jspa?threadID=707176

          Vyacheslav
          • 2. Re: Loading system libraries from java at runtime.
            711297
            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.
              user2099262
              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");
                             field.setAccessible(true);
                             String[] paths = (String[])field.get(null);
                                      for (String path : paths) {
                                          if (classpath.equals(path))
                                              return;
                                     }
                             String[] tmp = new String[paths.length+1];
                             System.arraycopy(paths,0,tmp,0,paths.length);
                             tmp[paths.length] = classpath;
                             field.set(null,tmp);
                        } catch (IllegalAccessException e) {
                                  e.printStackTrace();
                        } catch (NoSuchFieldException e) {
                                  e.printStackTrace();
                        }
                   }
              Edited by: user2099262 on May 11, 2010 12:03 AM

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