Java.lang.IllegalAccessError when calling java from scala - solutions / workarounds?

I am using the Java linear algebra library (ojalgo 32.0) for a scala project, and I ran into a strange problem. Each ojalgo method that I used works fine (for example, matrix and element multiplication, inverse and random matrix generation), with the exception of two seemingly simple matrix sizes. I have never had a problem calling java libraries before, and I am very interested in what happens. Here is a sample code in java that works fine:

public static void main(String[] arg) { MatrixFactory tmpFactory = PrimitiveMatrix.FACTORY; BasicMatrix wMat = tmpFactory.makeRandom(5,5,new Weibull(5.0, 2.0)); System.out.println(wMat.getColDim()); } 

Simple translation into scala (using version 2.9.2):

 object DataGen { def main(args:Array[String]):Unit = { val tmpFactory = PrimitiveMatrix.FACTORY val wMat = tmpFactory.makeRandom(5,5,new Weibull(5.0, 2.0)) println(wMat.getColDim) } 

The scala code calls the following:

 Exception in thread "main" java.lang.IllegalAccessError: tried to access class org.ojalgo.access.Structure2D from class DataGen$ at DataGen$.main(DataGen.scala:11) at DataGen.main(DataGen.scala) 

Line 11

 println(wMat.getColDim). 

A search for other cases of this error indicates that there are some known issues with access errors in scala, but I'm still not sure what the cause of the problem or the easiest workaround might be.

+4
source share
2 answers

I don’t quite understand why this does not work, but I played with it a bit and found that BasicMatrix not available, because I tried to explicitly specify the Matrix, because that's where getColDim . However, this worked when I threw the matrix onto PrimitiveMatrix :

 scala> res1.asInstanceOf[PrimitiveMatrix].getColDim res9: Int = 5 
+1
source

I just tested with Scala 2.10-M3 and ojalgo 32.4 and your code is working properly, so it looks like an error in earlier versions of scalac (compiling with 2.9 gives an error at runtime 2.10, but compiling with 2.10 does not give an error with runtime 2.9).

This suggests a simple workaround - updating the Scala version!

+1
source

Source: https://habr.com/ru/post/1413851/


All Articles