I have a scenario where I have to generate Java code when my application is running.
Can compile and run than code from my application( as a separate application).
You can use an instance of JavaCompiler:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
Follow the link for the an example on how to use it.