I tried below code but that is a java code and works only for.java files.
try {
JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager sjfm = jc.getStandardFileManager(null, null, null);
File javaFile = new File("/home/rishi/Desktop/Test.java");
Iterable fileObjects = sjfm.getJavaFileObjects(javaFile);
String[] options = new String[]{
"-d", "/home/rishi/Desktop/bin"
};
jc.getTask(null, null, null, Arrays.asList(options), null, fileObjects).call();
sjfm.close();
System.out.println("Class has been successfully compiled");
URL[] urls = new URL[]{new URL("file:///home/rishi/Desktop/")};
URLClassLoader ucl = new URLClassLoader(urls);
Class clazz = ucl.loadClass("PostProcessing");
System.out.println("Class has been successfully loaded");
Method method = clazz.getDeclaredMethod("call", null);
Object object = clazz.newInstance();
method.invoke(object, null);
} catch (Exception ex) {
ex.printStackTrace();
}
I want Scala code for the same.
I also tried below code which takes .class file which can be generated by either Java or Scala.
URL[] urls = new URL[]{new URL("file:///home/rishi/Desktop/")};
URLClassLoader ucl = new URLClassLoader(urls);
Class clazz = ucl.loadClass("PostProcessing");
System.out.println("Class has been successfully loaded");
Method method = clazz.getDeclaredMethod("call", null);
Object object = clazz.newInstance();
method.invoke(object, null);
Still, I faced issues with .class file generated by Scala. It is working for Java .class file.
PostProcessing.Scala :
class PostProcessing {
def call = println("Hurray !! You called me")
}
PostProcessing.java
public class PostProcessing {
public void call() {System.out.println("Hurray !! You called me");}
}
Update :
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
Please suggest me the solution.