Need to load a scala class at runtime from a jar and initialize it

蓝咒 提交于 2019-12-23 18:04:20

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/4119909/need-to-load-a-scala-class-at-runtime-from-a-jar-and-initialize-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!