1

I have a JAR file with java classes, which I want to use in my scala application. I don't want to add the JAR to the lib/ folder because I do not want to rebuild my scala application every time the JAR is changed.

So I tried my hands with scala reflections and class loaders and I was able to load the class, but I don't know how to use the methods of the class further in my scala application.

Here is my attempt so far,

scala> var classLoader = new java.net.URLClassLoader(Array(new File("java-module.jar").toURI.toURL),this.getClass.getClassLoader)
classLoader: java.net.URLClassLoader = java.net.URLClassLoader@130b3958

scala> var javaClass = classLoader.loadClass("com.sample.myClass")
javaClass: Class[_] = class com.sample.myClass

scala> javaClass.getMethods.foreach(println)
public java.lang.String com.sample.myClass.myMethod(java.lang.String)
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()

I want to use the method myMethod from the newly loaded class.

How can I go further to create an object of this particular class and use it's methods?

Edit: Added output for each line of code executed

2
  • 1
    this is wrong on so many levels... you should never build with reflection if you can avoid it. Use maven or sbt and insert you dependency in there. Manual building is as bad as building with reflection. Commented Feb 2, 2017 at 16:25
  • I had to keep the jar and my scala application separately because the jar is subject to future changes, where as my scala application is not going to change much. So just by restarting the application I can bring in the changes instead of rebuilding it every time. Commented Feb 2, 2017 at 16:31

2 Answers 2

1

Since you want to be able to load the jar on restart, you don't need to mess with reflection. Just have your java jar somewhere on the classpath outside of your scala jar. When you update the jar just replace it with the new version. As long as there were no breaking changes you won't need to rebuild the scala jar.

Sign up to request clarification or add additional context in comments.

2 Comments

I am relatively new to building scala applications. Which classpath do I have to add this to in order to build the application? And how?
The classpath of the application when you run it. The easiest way is to add Class-Path: ./* to the manifest.mf file in your jar, which will add the jars in the current directory into the ClassPath. You can also specifiy a specific path if you like. you could also use java -cp <classpath items> <main class> to launch the program. Keep in mind that you will need to add your scala jar in the classpath list as well using this method.
1

If the loaded class has a type unknown at runtime (e.g. it doesn't implement an interface known by your Scala code) you can only use reflection to access fields and run methods.

How can I go further to create an object of this particular class

If the class has a public no-arg constructor, you can construct it with javaClass.newInstance(), else you need to find the right constructor (similar like you did when listing the methods, there are several ways) and call it. See http://tutorials.jenkov.com/java-reflection/constructors.html for details

and use it's methods?

You get the proper Method object from the class, and call it like myMethod.invoke(myObject, args). See http://tutorials.jenkov.com/java-reflection/methods.html for details.

1 Comment

Sorry I am not able to understand it clearly. Since the class is unknown to scala, it cannot get what is inside the class at run time? I have edited the question to include the outputs, if that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.