2

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!

5
  • 1
    What exactly do you want? Dynamic loading of modules? Dynamic compilation of code? Commented Nov 6, 2012 at 20:07
  • I would say both. The user has the possibility to write some java code, compile it and run it from within the main application. The source and the class are saved in a library so that the user can run/change it anytime. The modules should be able to handle data from the main app and the main app should be able to process the results from the module. Commented Nov 6, 2012 at 20:29
  • docs.oracle.com/javase/6/docs/technotes/guides/javac/index.html should help you for compilation purposes.You need to write your own Class loaders for loading the bytecode and hot swapping. Commented Nov 6, 2012 at 20:47
  • 1
    It is certainly possible but Java doesn't seem to be a good fit for this use case. Have you considered using the scripting API? Commented Nov 6, 2012 at 20:56
  • @biziclop Thanks, the scripting API looks great! Why don't you think that Java is a good fit? Which language would you recommend? I am using Linux. Commented Nov 6, 2012 at 21:21

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Hakan, thank you for your suggestions. But I think this is a bit to heavy for my purposes :-)
I do not know your context, but nevertheless I recommend you to check this link for reviewing some of the popular misconceptions about OSGi (such as the misconception of OSGi being heavyweigth): jroller.com/habuma/entry/a_dozen_osgi_myths_and
1

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.

1 Comment

Thanks, that sounds good. I was not aware of groovy and that a javascript interpreter is part of the JDK. This may be a possibility.
0

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.

1 Comment

Thanks, I just tried using the Java Scripting Engine and Jython. Looks promising! Just what I needed. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.