I am looking for some guidance to load the scala jar into the java classloader. Below function works for me when I use a java jar file.
where, arr is an Array of java.net.URL for all the jars that I need to load into the classloader.
val classLoader = new URLClassLoader(arr, this.getClass().getClassLoader())
val clazz = classLoader.loadClass(className)
val myMethod = clazz.getDeclaredMethod(methodName, classOf[String])
myMethod.setAccessible(true)
val response = myMethod.invoke(methodName)
However, when I am trying to use similar method to call a scala function from a scala jar file, I get classNotFound Exception at val clazz = classLoader.loadClass(className) at this line.
val clazz = classLoader.loadClass("com.testing.Test")
I want to call print method of class Test. Can someone please guide? I checked couple of other posts in SO, but they are dealing with objects and traits which may not be applicable for my use case.
Scala class for example:
package com.testing
class Test(){
def print (a: String, b: String): String = {
return a+b
}
}
Thanks!
Posting the answer for someone looking for a similar issue. Classloader does work as expected for both scala and java.
For Java, just passing the method name in method.invoke worked for me, while in scala I had to specifically create an object of class and pass that object to method.invoke. A new learning for me.