dll loading problem

  • Thread starter Dmitry Yukelson
  • Start date
D

Dmitry Yukelson

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:already loaded in another classloader

Thanks in advance
 
G

Gordon Beaton

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:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top