In c#, you can use System.Reflection.Emit.DynamicMethod to generate compiled code at runtime. Is there equivalent functionality in java? I know that to use a compiler, you need a JDK, but if you only have a JRE, is there a way to generate compiled code at runtime?
Here are two links that may shed some light on this issue. I have them, but I did not play around with this for years, so I don't think I could answer further questions. I remember I could do this and there were some complications, as some SDK only jar has to be included.
Thank you baftos and rp0428:
baftos, both solutions need the JDK to compile files. I need to provide compilation using only JRE.
rp0428, the c# System.Reflection.Emit.DynamicMethod provides a way to construct source code using low-level byte code instructions, and then generate compiled code. This is a way to provide on-the-fly compilation. Is there an equivalent funtionality in java to makeup for not having the regular comiler in the JRE.
Just to be clear "System.Reflection.Emit.DynamicMethod" is used to construct a method (not class) using C# (CIL) byte code.
There is no equivalent to that in the standard API including the java compiler.
The java compiler compiles java into a java class. There are other, different, equivalents to that in C#.
There are third party libraries that allows one to create a java class using java byte codes. Those would be the closest approximation to the DynamicMethod.