Some days ago, i got a requirement that i need to load an external scala file in my Scala application and have to use that. To use that file, it needs to be compile first and then can be used in the application.
The location of that external file can be anything apart from your project.
The structure of external file is fixed and should be define in your project i.e. class name and method signature.
Arguments and return type of the method can be vary according to our requirement.
Firstly we will define the dependencies in build.sbt :
Now, we need to define the code which will compile and load the external file in our application.
filePath is the path of external file which needs to be loaded.
You will notice the import statement as well :
This import is there because abstract class ExternalProcessing needs to be import in external file code as well otherwise type ExteranlProcessing not found compile time error will be there.
Now, we need to use externally loaded file as below:
Full code :
Get full code here
Hope this will be useful for you.
Happy Blogging. !!!
2 thoughts on “Compile & load an external scala file in your application2 min read”
Reblogged this on Rishi Khandelwal.
Thanks for this blog Rishi. I want to add the generated class to some particular package (in my case I am trying to extend some class which is not accessbile in the current package). But I am not able to do it (code/syntax) and don’t have an answer to why it is not possible.
Comments are closed.