Chris
Chris

Reputation: 537

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

I'm using a java linear algebra library (ojalgo 32.0) for scala project, and I've encountered a strange problem. Every ojalgo method I've used works fine (e.g., matrix and element-wise multiplication, inverse, and random matrix generation) except for two seemingly simple ones for getting matrix dimensions. I've never had any trouble calling java libraries before, and I'm pretty curious about what's going on. Here's some example code in java, which 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());
}

A simple translation to 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 throws this:

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 is

println(wMat.getColDim).

Searching 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 cause of the problem or the cleanest workaround might be.

Upvotes: 5

Views: 695

Answers (2)

rxg
rxg

Reputation: 3982

I just tested with Scala 2.10-M3 and ojalgo 32.4 and your code works as expected, so it looks like a bug in earlier versions of scalac (compiling with 2.9 gives the error with the 2.10 runtime, but compiling with 2.10 doesn't give the error with the 2.9 runtime).

This suggests an easy workaround - upgrade Scala version!

Upvotes: 1

drexin
drexin

Reputation: 24403

I don't exactly understand why it does not work, but I played around a bit with it and found, that BasicMatrix is not accessible, as I tried to cast the Matrix explicitly to it, because that is where getColDim is implemented. However it worked, when I casted the Matrix to PrimitiveMatrix:

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

Upvotes: 1

Related Questions