2

I am developing a scala compiler plugin. The compiler plugin runs after the "refcheck" phase. It analyzes the AST(Abstract Syntax Tree) and generates some output. In this plugin, I am loading a native library "scalaz3.dll" using System.loadLibrary

I plan to use the compiler plugin with the Eclipse Scala IDE. I set the -XPlugin parameter in the ScalaIde preferences. When I compile a scala source file in Eclipse, the plugin is invoked after the refchecks phase and works fine (generates the desired output)

However, when I make changes to the the source file and compile, I am getting the following error.

The SBT builder crashed while compiling your project. 
Native Library scalaz3.dll already loaded in another classloader.

Looks like the ScalaIDE generates another classloader and tries to load the plugin classes in the same process.

I was facing the same problem in the compiler plugin test case. However, I fixed it using fork in Test := true in the build.sbt. But I am clueless about how to fix it in eclipse scala IDE.

Here are few related posts(although not related to eclipse scala ide):

Community
  • 1
  • 1
dips
  • 1,627
  • 14
  • 25

1 Answers1

2

The IDE uses sbt in-process, so there is no equivalent of forking. Couldn't you load the dll only once, for instance by loading it inside the constructor of an object?

The technique is suggested here:

The class that calls System.loadLibrary(String) must be loaded by a classloader that is not affected by reloading the web application itself.

Thus, if you have JNI code that follows the convention of including a static initilaizer like this:

class FooWrapper {
    static {
        System.loadLibrary("foo");
    }

    native void doFoo(); 
} 

[...]

laughedelic
  • 6,230
  • 1
  • 32
  • 41
Iulian Dragos
  • 5,692
  • 23
  • 31