3

To all -- I'm probably at best a new guy here, trying to wrap my head around scala, and I find I need to do the following:

  • Assume I have a scala class on disk somehwere referenced in my classpath.
  • I have a scala application that wants to dynamically load this class and call its constructor
  • Once I have that class reference, I can use it to set up values in other classes and objects.

In Java, I'd use the Java class loader and create a new instance whereupon I'd call its constructor. What is the right way to do this in Scala?

user500123
  • 617
  • 6
  • 14

2 Answers2

4

Scala classes are Java classes, so just do what you'd do in Java. The Scala Java Interoperability FAQ doesn't talk about classloaders specifically, but it might be helpful as you figure things out.

Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
2

I have written a blog entry quite a long time ago on this. Unfortunately I haven't found the time to update it for Scala 2.8 .

Essentially it boils down to

  • do it like you would in Java
  • use Scala features to improve the user interface
mkneissl
  • 4,902
  • 2
  • 26
  • 28