jeudi 26 juillet 2018

How to call a scala function with parameters using java classloader?

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!





Aucun commentaire:

Enregistrer un commentaire