Fixing sbt error: java.lang.UnsatisfiedLinkError: Native Library C:\Users\gary\.javacpp\cache\nd4j-native-0.9.1-windows-x86_64.jar\org\nd4j\nativeblas\windows-x86_64\jnind4jcpu.dll already loaded in another classloader

If you write code with ND4S and try to run it with “~run”, you’ll get the following error, subsequent to the first run.

The fix to this is to add this line to your build.sbt, which causes a new JVM to run on each run:

fork := true

Error:

java.lang.ExceptionInInitializerError
        at org.nd4j.linalg.cpu.nativecpu.ops.NativeOpExecutioner.(NativeOpExecutioner.java:53)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at java.lang.Class.newInstance(Class.java:442)
        at org.nd4j.linalg.factory.Nd4j.initWithBackend(Nd4j.java:6185)
        at org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:6087)
        at org.nd4j.linalg.factory.Nd4j.(Nd4j.java:201)
        at org.nd4s.Implicits$IntArray2INDArray$.asNDArray$extension(Implicits.scala:48)
        at Main$.main(Main.scala:10)
        at Main.main(Main.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
Caused by: java.lang.RuntimeException: ND4J is probably missing dependencies. For more information, please refer to: http://nd4j.org/getstarted.html
        at org.nd4j.nativeblas.NativeOpsHolder.(NativeOpsHolder.java:51)
        at org.nd4j.nativeblas.NativeOpsHolder.(NativeOpsHolder.java:19)
        at org.nd4j.linalg.cpu.nativecpu.ops.NativeOpExecutioner.(NativeOpExecutioner.java:53)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at java.lang.Class.newInstance(Class.java:442)
        at org.nd4j.linalg.factory.Nd4j.initWithBackend(Nd4j.java:6185)
        at org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:6087)
        at org.nd4j.linalg.factory.Nd4j.(Nd4j.java:201)
        at org.nd4s.Implicits$IntArray2INDArray$.asNDArray$extension(Implicits.scala:48)
        at Main$.main(Main.scala:10)
        at Main.main(Main.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
Caused by: java.lang.UnsatisfiedLinkError: no jnind4jcpu in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
        at java.lang.Runtime.loadLibrary0(Runtime.java:870)
        at java.lang.System.loadLibrary(System.java:1122)
        at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:976)
        at org.bytedeco.javacpp.Loader.load(Loader.java:777)
        at org.bytedeco.javacpp.Loader.load(Loader.java:684)
        at org.nd4j.nativeblas.Nd4jCpu.(Nd4jCpu.java:10)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at org.bytedeco.javacpp.Loader.load(Loader.java:739)
        at org.bytedeco.javacpp.Loader.load(Loader.java:684)
        at org.nd4j.nativeblas.Nd4jCpu$NativeOps.(Nd4jCpu.java:63)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at org.nd4j.nativeblas.NativeOpsHolder.(NativeOpsHolder.java:29)
        at org.nd4j.nativeblas.NativeOpsHolder.(NativeOpsHolder.java:19)
        at org.nd4j.linalg.cpu.nativecpu.ops.NativeOpExecutioner.(NativeOpExecutioner.java:53)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at java.lang.Class.newInstance(Class.java:442)
        at org.nd4j.linalg.factory.Nd4j.initWithBackend(Nd4j.java:6185)
        at org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:6087)
        at org.nd4j.linalg.factory.Nd4j.(Nd4j.java:201)
        at org.nd4s.Implicits$IntArray2INDArray$.asNDArray$extension(Implicits.scala:48)
        at Main$.main(Main.scala:10)
        at Main.main(Main.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
Caused by: java.lang.UnsatisfiedLinkError: Native Library C:\Users\gary\.javacpp\cache\nd4j-native-0.9.1-windows-x86_64.jar\org\nd4j\nativeblas\windows-x86_64\jnind4jcpu.dll already loaded in another classloader
        at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1907)
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1824)
        at java.lang.Runtime.load0(Runtime.java:809)
        at java.lang.System.load(System.java:1086)
        at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:956)
        at org.bytedeco.javacpp.Loader.load(Loader.java:777)
        at org.bytedeco.javacpp.Loader.load(Loader.java:684)
        at org.nd4j.nativeblas.Nd4jCpu.(Nd4jCpu.java:10)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at org.bytedeco.javacpp.Loader.load(Loader.java:739)
        at org.bytedeco.javacpp.Loader.load(Loader.java:684)
        at org.nd4j.nativeblas.Nd4jCpu$NativeOps.(Nd4jCpu.java:63)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at org.nd4j.nativeblas.NativeOpsHolder.(NativeOpsHolder.java:29)
        at org.nd4j.nativeblas.NativeOpsHolder.(NativeOpsHolder.java:19)
        at org.nd4j.linalg.cpu.nativecpu.ops.NativeOpExecutioner.(NativeOpExecutioner.java:53)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at java.lang.Class.newInstance(Class.java:442)
        at org.nd4j.linalg.factory.Nd4j.initWithBackend(Nd4j.java:6185)
        at org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:6087)
        at org.nd4j.linalg.factory.Nd4j.(Nd4j.java:201)
        at org.nd4s.Implicits$IntArray2INDArray$.asNDArray$extension(Implicits.scala:48)
        at Main$.main(Main.scala:10)
        at Main.main(Main.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)