Compile & load an external scala file in your application

Table of contents
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 tech enthusiast with having around 10 years of experience who loves to solve complex problems with pure quality. He is a functional programmer and loves to learn new trending technologies. His leadership skill is well prooven and has delivered multiple distributed applications with high scalability and availability by keeping the Reactive principles in mind. He is well versed with Scala, Akka, Akka HTTP, Akka Streams, Java8, Reactive principles, Microservice architecture, Async programming, functional programming, distributed systems, AWS, docker.

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.