user1204121
user1204121

Reputation: 385

Dynamically load and run Java code from within Java application

I am thinking about writing a Java application where the user can write some modules in Java and add them to a library in this application. The modules will be some calculations that use data from the main app. Maybe a bit like VBA in Office.
I would appreciate if someone would give me some hints where to start as I couldn't find something useful on the net.
Thanks in advance!

Upvotes: 2

Views: 340

Answers (3)

Matt
Matt

Reputation: 11815

you might be better off incorporating some form of jvm scripting language. Something like groovy, jruby or jython. Those don't need actual compilation and can be stored in your system as source. Plus they can be quite nice to write code in too. Groovy has the advantage of being a superset of java. (For the most part) you could just degrade to java code, and run it in the groovy interpreter.

Upvotes: 0

AlexR
AlexR

Reputation: 115398

Theoretically it is possible. You can allow user to write java classes, then you can compile the class using java compiler, generate .class files. you can load them using your custom class loader (probably URLClassLoader or its subclass, etc.

BUT It is very serious application. Actually it is a kind of IDE. So, of you really want this check out a possibility to create Eclipse based application, i.e. implement several eclipse plugins.

Other approach may be to allow user wring code in one of popular scripting languages. For example groovy that has java-like syntax but can be run without compilation and does not require creating classes etc. Javascript is an option too. Javascript interpreter is a part of JDK, so you even do not need external dependencies.

Upvotes: 1

Hakan Serce
Hakan Serce

Reputation: 11266

You can try to develop a module framework from scratch, if you wish. However, if you are developing anything serious you should consider using OSGi.

Upvotes: 1

Related Questions