2

I need to compile some code in java while runtime. I looked up this and it looks like the way to do it. So far so good but I need to run the final program on every platform. So I'm getting in troubles if there is a system with a jre only.

Now I thought about getting a compiler as a java lib. But I'm also not sure about the solution of copying the tools.jar from a java jdk into my project. Is it a valid approach or is there another solution or maybe a lib which I can use?

I need this because I want to translate a script into java code and execute it.

Thank you!

-- edit --

Why I need this?

The user should has influence on the behavior of some parts of my program. So there is something like a little script language. Because of performance reasons I think it would the best solution to translate it into java source code and then into bytecode and use it then. This script will not change often and only before the main part of the program runs. If there are better alternatives I would be happy to know about them, too.

If there are any questions or if you need more information about specifics just ask.

H3ADLESS
  • 83
  • 1
  • 10
  • 1
    You normally need a JDK to compile code. What code do you need to compile at runtime and why? – Elliott Frisch Dec 04 '14 at 21:31
  • Well, why don't you require a JDK then? There is also Eclipse ecj but I've never used it – fge Dec 04 '14 at 21:32
  • @ElliottFrisch - I have a special language for people wich aren't comfortable with programming. It's for defining some attributes and functions wich controls other parts of the program. – H3ADLESS Dec 04 '14 at 21:35
  • @H3ADLESS And what if they don't have a JRE? – Elliott Frisch Dec 04 '14 at 21:35
  • @fge That would be a solution but I want to keep it as simple as possible by using my program because of the reasons above. – H3ADLESS Dec 04 '14 at 21:36
  • @ElliottFrisch I'm not shure if I am making a mistake but if someone install Java from [this page](https://www.java.com/de/download/) they get a jre as simple as possible, didn't they. So that's simple enough (I hope). – H3ADLESS Dec 04 '14 at 21:41
  • I agree, but these are people *which [sic] aren't comfortable with programming*. Regardless, you could write an interpreter for your DSL; I don't understand why it needs to compile to Java at runtime. – Elliott Frisch Dec 04 '14 at 21:52
  • Note that JSR-223 allows to invoke embedded languages (like javascript or what else you put in the classpath) with scripts you provide. – Thorbjørn Ravn Andersen Aug 30 '15 at 12:27

1 Answers1

1

The best suggestion I can come up with is Janino: http://docs.codehaus.org/display/JANINO/Home. Unfortunately, the compiler seems to not yet fully support Java 5 (based on what the home page says), so you may have issues to address.


I think your best bet is to bite the bullet and make a JDK a prerequisite. It should not be significantly more difficult to install a JRE than a JDK, and explaining to people that they don't need to do Java development is easy.

There are other alternatives that avoid the problem:

  • Instead of generating Java source code, compile directly to bytecodes. (Not that I would recommend doing this ...)

  • Compile your DSL to an internal form that you can write an interpreter for.


Because of performance reasons I think it would the best solution to translate it into java source code and then into bytecode and use it then.

That sounds like "premature optimization" to me.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thank you for your answer. Janino seems to be something like I am looking for. Are there any alternatives? Your first alternative means that I have to compile by myself into Java bytecode? That sounds like much effort. – H3ADLESS Dec 05 '14 at 09:59
  • I already did. It's difficult to find something but that didn't means there is nothing. I think I have my answer. Thank you. – H3ADLESS Dec 05 '14 at 10:10