4

I am working on a program, which is supposed to have "Modules" which people can make their own code from an API, and they can then add one of their Modules, let's say it's called ModuleNicerText, they add it to the MyProgram\Modules folder as a .java class (the source), and the class implements Module.

The Module class has one method (well, it has more but for the sake of this post I'll say one), called onUpdate(), and I want to be able to somehow import that class to my code, and then run the .onUpdate() whenever I need to, I hope I explained it well enough, if I haven't then here is another example:

I have an interface, named Module

this is the body of Module:

public interface Module {
    void onUpdate();
}

Now, I have my main class named MyProgram, and in that class I have this method:

public void onUpdate(){
    for (Module module : modules){
        module.onUpdate();
    }
}

I use this kind of system, so I can easily add or remove components without having to mess with a bunch of stuff, if it is all crowded in one class/method.

Now, there is a directory that gets created when the program is run which is MyProgram\Modules and anyone who wants can add a .java file which extends the Module interface there.

I want to be able to look through the .java files in that directory and be able to call the .onUpdate() method for them, so that my users can have as much customization as they possibly can!

Any help at all is very much appreciated, and I thank you in advance for taking your time to read this enormous wall of text :)

  • 1
    Why are you putting such burden on you? As the owner of the API, why is it important that users put their code in specific directory? I don't recall any API that puts this kind of restriction on its users? Why should you be different? – hfontanez Nov 17 '14 at 23:09
  • 1
    @hfontanez I don't think the path actually matters that much. The real question here is the OP needs to compile the source file and make an object from it. – Radiodef Nov 17 '14 at 23:13
  • 1
    @Radiodef maybe you don't think that, but that is exactly what the OP wrote. What I was trying to do is to maybe restate his problem, or rethink his/her strategy. – hfontanez Nov 17 '14 at 23:24
  • 1
    @hfontanez I guess my point is *"import and use"* is a much bigger question than *"from a specified path"*. – Radiodef Nov 17 '14 at 23:25
  • @Radiodef you got it! that is the real question... I need to be able to compile it and then call the methods (make an object from it..) –  Nov 17 '14 at 23:26
  • 2
    possible duplicate of [How do you dynamically compile and load external java classes?](http://stackoverflow.com/questions/21544446/how-do-you-dynamically-compile-and-load-external-java-classes) – hankd Nov 18 '14 at 00:06

2 Answers2

0

There is a good article (a little old, but great explanation). It provides detailed information about how to use the Proxy Pattern through UpToDate file check. By the time you keep the same methods inside your interface, and change only implementation of it, you can make use of this technique only in classes which implements the interface.

Check it out: Dynamic Java code to your Application

  • Thank you much! I am currently on phone so I cannot test anything in the article, but I have read most of it and it seems very promising! Thanks a lot again :) –  Nov 18 '14 at 11:55
0

You can use Java Simple Plugin Framework. It does something similiar to what you want and is ready to use.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162