How to call a function from a java library

Your problem is actually with the java.util.List API. Its toArray method returns a Array[AnyRef].

You can use this other toArray method, which I find quite ridiculous to be honest:

ManagementFactory
  .getGarbageCollectorMXBeans()
  .toArray(Array.empty[GarbageCollectorMXBean])

Or you can move to the Scala universe where toArray does what one would expect:

import scala.jdk.CollectionConverters._

ManagementFactory
  .getGarbageCollectorMXBeans()
  .asScala
  .toArray