.dll already loaded in another classloader?
Asked Answered
M

4

22

I have a webapp running under Tomcat 3.2.1 that needs to make JNI calls in order to access data and methods in legacy C++ code. A servlet is loaded on startup of the webapp that, as part of its init method, causes a data set specific to that webapp instance to be loaded into the C++ data structures.

This Java code for this servlet contains the following:

static
{
    try {
        System.loadLibrary("JCoreImpl");
        System.out.println("JCoreImpl loaded");
        m_bLibraryLoaded = true;
    } catch (UnsatisfiedLinkError e) {
        m_bLibraryLoaded = false;
        System.out.println("JCoreImpl NOT loaded " + e);
    }
}

Things work fine if there is only one webapp (let's call it "webapps/aaa").

If I have a second webapp ("webapps/bbb") that is identical to webapps/aaa except for the data set used in the C++ data structures, then webapps/aaa starts up just fine, but when webapps/bbb is started I get an error stating that:

JCoreImpl NOT loaded java.lang.UnsatisfiedLinkError: Native Library
E:\WebStation\binDebug\JCoreImpl.dll already loaded in another classloader

I need to have a separate instance of the native library for each of my webapps as each instance needs to contain data that is unique to that particular webapp. I have searched through the mail archives and read emails by Craig McLanahan explaining the classloader hierarchy. But I have not been able to find anything specific to loading a unique instance of a native library for each webapp.

Monocle answered 23/6, 2009 at 5:44 Comment(0)
S
16

You can't load the same native library twice.

Put the class in a jar file under <tomcat>/lib/, it will be shared over all wars.

Sabah answered 23/6, 2009 at 6:51 Comment(0)
S
9

See the section I'm encountering classloader problems when using JNI under Tomcat on the Tomcat HowTo wiki (http://wiki.apache.org/tomcat/HowTo)

Schopenhauer answered 8/7, 2009 at 23:34 Comment(0)
H
0

Copy the DLLs to temporary files in a temporary directory before loading them. Delete the files when you are done. This way you can ensure that the same DLL is not loaded twice.

Hearttoheart answered 11/11, 2015 at 10:27 Comment(0)
T
0

I had this problem and I solved it:

Problem:

The problem is due to some configurations of your application server which cause to create more than one war or ear files and consequently it creates more than one deployed files. These deployed files attempt to have a concurrent access to your native library which loaded in a static block.

Solution:

  1. Stop your Application Server and close the IDEA
  2. Finish all java processes from task manager
  3. Go to this address of your App Server: jboss-eap-6.4.0\standalone\configuration and open standalone.xml file
  4. In the standalone.xml , delete all the deployments tag (Don't worry, when you rerun the application server, it inserts this tag
  5. Go to this address of your App Server: jboss-eap-6.4.0\standalone\deployments and delete all deployed files and war files. (You can backup your war files)
  6. Open IntelliJ Idea and go to: Edit Configuration JBOSS -> Deployment tab
  7. Delete current deployment and add a new one that should be exploded
Tusche answered 24/8, 2016 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.