dll loading problem

Discussion in 'Java' started by Dmitry Yukelson, Sep 7, 2004.

  1. Hi all,

    I'm trying to create an interactive application which compiles ant
    file, and does several other things.
    For this purpose I'm using Project and ProjectHelper ant classes.
    In addition, I defined my own ant Task, which used native code:

    public class IdentityTask extends MatchingTask {
    ....
    private native boolean CallIdentity();

    static {
    System.loadLibrary("...");
    }
    ......
    }


    The first compilation is succeeded without any problems, the dll is
    loaded and the native code is called. But when the second compilation
    request is processed, the following exception is thrown:
    >>>>sample.xml java.lang.UnsatisfiedLinkError: Native Library ....

    already loaded in another classloader

    Thanks in advance
    Dmitry Yukelson, Sep 7, 2004
    #1
    1. Advertising

  2. On 7 Sep 2004 04:08:07 -0700, Dmitry Yukelson wrote:
    > For this purpose I'm using Project and ProjectHelper ant classes. In
    > addition, I defined my own ant Task, which used native code:
    >
    > public class IdentityTask extends MatchingTask {
    > ....
    > private native boolean CallIdentity();
    >
    > static {
    > System.loadLibrary("...");
    > }
    > .....
    > }
    >
    > The first compilation is succeeded without any problems, the dll is
    > loaded and the native code is called. But when the second compilation
    > request is processed, the following exception is thrown:
    >>>>>sample.xml java.lang.UnsatisfiedLinkError: Native Library ....

    > already loaded in another classloader


    The exception says that you attempted to load the same class (and
    consequently, its native library) a second time while the initial
    library was still loaded.

    I don't know how Ant tasks are structured, or how Ant assigns
    classloaders to them etc, however the following holds in general when
    you use native code in your application:

    - any given native library can only be loaded once
    - a native library can't be unloaded explicitly by your code, and
    won't be unloaded by the JVM until its classloader is garbage
    collected.

    So you need to ensure that your classes with native methods are loaded
    high enough in the classloader hierarchy that they are visible to any
    other classes that will need them.

    In your case, you could consider factoring out the native part into a
    class of its own that you can load separately (once), and then make
    calls to it from your IdentityTask.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Sep 7, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anushi
    Replies:
    5
    Views:
    8,640
  2. =?Utf-8?B?TkdN?=

    ASP.NET: problem loading managed C++ DLL

    =?Utf-8?B?TkdN?=, Feb 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    829
    Simon Kennedy
    Feb 9, 2004
  3. Replies:
    2
    Views:
    327
  4. Adam M. Rosenzweig

    Problem with .NET WebService Loading Unmanaged DLL

    Adam M. Rosenzweig, Mar 6, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    183
    Adam M. Rosenzweig
    Mar 6, 2006
  5. H. Simpson
    Replies:
    4
    Views:
    250
    H. Simpson
    Aug 3, 2004
Loading...

Share This Page