Compile & load an external scala file in your application

Reading Time: 2 minutes

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. !!!

Written by 

Rishi is a Lead Consultant, with experience of more than 7 years. Rishi is product focused developer who loves developing both front-end user interfaces and scalable back-end infrastructure. He is a good team player, quick learner and a humble person. He has good time management skills, aimed to give best results and fully dedicated towards his work & responsibilities. He is able to work as individual and as well as in team. He loves to share his knowledge, therefore he often writes technical blogs.

2 thoughts on “Compile & load an external scala file in your application2 min read

  1. 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.

%d bloggers like this: