Dynamic loading of the .DLLs - hitting a snag

Calling net.java.games.jogl.impl.NativeLibLoader.disableLoading() before doing any JOGL-related operations will avoid the exception above.

In the past two years there has only been one other request to do something similar to what you’re doing (which is why the enableLoading/disableLoading methods are exposed). I think most people are deploying JOGL apps with Java Web Start which takes care of this automatically. During development it isn’t difficult to write some simple wrapper scripts which set up either the PATH/LD_LIBRARY_PATH/CLASSPATH environment variables or -Djava.library.path/-classpath.

That did it! Once I get the .jar loading situation handled with a classloader, I’m all set.

You really are a ‘ninja’, Ken. Thanks much.

HI, i am also interested in dynamically loading those libs.
Want to use this for my engine. Tried this many times but no.
Will there maybe a built-in LibLoader in JSR-231 or standart loader?

Feel free to file a Request for Enhancement using the JOGL Issue Tracker.

Can you tell me what the Tools class is.

Consider it done:

https://jogl.dev.java.net/issues/show_bug.cgi?id=171
https://jogl.dev.java.net/issues/show_bug.cgi?id=172

It’s just a bunch of static utilities I wrote. Nothing major.

Here’s a version of the code, which now works, with the Tools calls replaced with straight java:


/** Static initialization  - load libraries */
      /** Ensure we load GL on startup. This is a replacement for the command-line switches
     *  -Djava.library.path=.
     *
     *  @param bLoadCg if 'true', load Cg libraries
     */
    public static void loadOpenGL(boolean bLoadCg)
    {         
        // workaround to allow our loader to do its job
        // see http://192.18.37.44/forums/index.php?topic=10262.0;topicseen
        net.java.games.jogl.impl.NativeLibLoader.disableLoading();
        
        // Manually load AWT to work around the problem with error boxes coming up claiming it's not found. 
        // Apparently AWT has to be loaded -before- jogl's .dll files or all Hell breaks loose.
        java.awt.Toolkit.getDefaultToolkit();
        System.loadLibrary("jawt");        
        
        // Now, load the .dll/.so files for the OpenGL C bindings 
        String strOS = System.getProperty("os.name");
        String strJOGL = System.getProperty("user.dir") + System.getProperty("file.separator") + "lib" + 
                System.getProperty("file.separator");
        
        if (strOS.startsWith("Windows"))
        {                        
            System.load(strJOGL + "jogl.dll");            
            
            if (bLoadCg)
                System.load(strJOGL + "jogl_cg.dll");                
        }        
        if (strOS.startsWith("Mac OS"))
        {
              System.load(strJOGL + "libjogl.jnlib");
              if (bLoadCg)
                System.load(strJOGL + "libjogl_cg.jnlib");        
        }                
        if (strOS.startsWith("Linux"))
        {
                System.load(strJOGL + "libjogl.so");
                if (bLoadCg)
                    System.load(strJOGL + "libjogl_cg.so");
        }
        if (strOS.startsWith("Solaris"))
        {
               String strArch = System.getProperty("os.arch");
               if (strArch.equalsIgnoreCase("sparc"))
               {
                   System.load(strJOGL + "libjogl_solsparc.so");
               }
               if (strArch.equalsIgnoreCase("x86"))
               {
                   System.load(strJOGL + "libjogl_solx86.so");
               }
        }
    }
  

Could this line of tought be extended so that the .so and .dll libs are loaded directly from a .jar ? And would it be possible to bake cg.dll (The Cg runtime) inside the .jar aswell ?

// Tomas :slight_smile:

It is not possible to System.load() a shared object directly from within a jar file. At a minimum it would have to be unpacked on to the local disk.

How’s about just a static call, passing in the path to your lib directory?

Bugger. Like I expected then.

I don’t know if that solution would be any better than just shipping the natives unpacked and I guess that java doesn’t allow you to mount a virtual filesystem in RAM memory either ?

// Tomas

// Tomas

It would still enable them to make a function to load the libs dynamically for the right os without the user needing to use -Djava.library.path.

Incidentally, does anyone know of a way to do the noddraw workaround in code?

True.

I suggested some extracting/loading procedure to someone a while ago http://192.18.37.44/forums/index.php?topic=10074.msg79958#msg79958
Still haven’t tried it though. Maybe if you combine it with File#deleteOnExit to avoid littering the disk with libraries it might work :slight_smile:

Ken, we’re still having trouble, with the code posted above, getting jawt loaded on OS X. Any more suggestions from your bag of tricks?

Could you please post the exception stack trace?

Sure, as soon as my tester with the Mac gets online :slight_smile:

Hello,
Thank you very much for the useful info you provide.
I tried the dynamic loading of the native libraries, and it works fine under windows and linux.

I had a problem on mac, but finally I managed to figure it out.
So it works now on mac as well!
The difference from Jaeden’s code is, that I do not load jawt on mac.
I had a lot of trouble from mismatching versions of jar and native files.
Hope this helps.

Hi,
i’m trying to do the same things by loading the DLLs and the jar file from my code but i’m not able.

Here is the code i use (same as Jaeden):


        //Allow loader
        NativeLibLoader.disableLoading();

        //Seems to have to load awt first
        java.awt.Toolkit.getDefaultToolkit();
        System.loadLibrary("jawt"); 
        
        //Load the jogl jar file
        URL urlJOGL = (new File(ThinClient.getPath() + "jogl.jar")).toURL();
        URL[] urls = { urlJOGL };            
        URLClassLoader classLoader = new URLClassLoader(urls);

        //Load the DLLs
        System.load(ThinClient.getPath() + "jogl.dll");
        System.load(ThinClient.getPath() + "jogl_awt.dll");

After this, i’m getting this error


java.lang.NoClassDefFoundError: com/sun/opengl/impl/NativeLibLoader
...

It was like if my jar wasn’t loaded. Anybody have an idea?

I want to do this cause on Windows i’m able to put the jar into the lib/ext folder and the DLLs into the bin folder of the JRE the first time, but i need a cross platform solution. On Linux i can’t do this, cause the JRE folders are 99% with root acces, so i cannont use this method with normal user. This solution seems good cause i put the files into the home directory of the user (i’ll adapt the code for others os after i’ll be able to load the stufss dynamically).

thx a lot!!!

I finaly use Java WebStart and that working very well

thx a lot
i’m leaving this topic