0

I'm trying to write a program that allows users to create "plugins". Kind of like how the Linux bash shell can have commands added by putting a file in a directory, I'm trying to allow users to put their code in a directory, and have my program execute it.

I thought of a few ways to do this:

  1. Somehow manage to create my own scripting language and have users use that to write their plugins
  2. Find an existing scripting language that Java supports and use that for users to write their plugins
  3. Have the users write their plugins in Java, and compile them during my program's runtime.

Though all 3 options seem pretty bad, this is all I could think of, and option 3 seemed the best of the three. So, I did some research and saw that Java 1.6 onward supported runtime compilation of files. However, if the code the users write references some specific classes that I have in the base program, I'm not sure if the compiler will fail. Normally it would, but since my program is compiling it, I don't know if the compiler will allow them to use the classes in the base program.

I'd ideally like this to be as easy for the user as possible. My main question is whether the compiler will have an issue with referencing classes in my base program. I don't want to have to include a .jar with all my classes and have the users use it as a Library when they're developing. As I said, I want this to be easy for the user, and ideally, it should be as close to an Interpreted Language as possible. That's why I'd like to do this at runtime in my program, so that the users don't have to bother with compiling. They just need to put the .java file in a directory and run my program.

If someone knows whether the compiler will have an issue with the classes, or if they know a better way to go about this, that'd be great!

Thanks!

Globmont
  • 881
  • 2
  • 8
  • 20

1 Answers1

1

Most commonly plugins for Java applications are all written in Java. Plugins are a tricky business because they expose a lot of potentially bad things.

If you want to compile a scripting language to Java then ok. You have to:

  1. Create the Java class http://www.javabeat.net/the-java-6-0-compiler-api/
  2. Establish a set of rules of what the script can and cannot do.
  3. Invoke the compiler w/ references to the API Jar you want to expose to that Java class (so the compiler can follow the reference chain)
  4. Load only the Java class and not the API jar into a Sub ClassLoader of the current ClassLoader. http://www.javaworld.com/article/2077260/learn-java/the-basics-of-java-class-loaders.html

There should be no problems with class referencing if you compile the target *.java with the same API jar that is currently loaded. DO NOT LOAD THE API JAR IN THE SUB CLASSLOADER because class type errors will occur.

Look at LuaJava for a Scripting engine.

http://www.keplerproject.org/luajava/manual.html

bond
  • 1,054
  • 8
  • 15