2

Is it possible to compile and emit .class files at run time? I have some generated servlet code and I want to compile them into classes and package it as a war.

Thanks.

Abhishek Dey Das
  • 577
  • 6
  • 20

2 Answers2

1

Yes, it is.

You can take a look at the Java Compiler API doc

Note however, that you will have to provide the corresponding ClassLoader and manage all the resources yourself.

If you want to generate bytecode from non-Java sources, you can also use ASM directly:

asgs
  • 3,718
  • 6
  • 36
  • 49
choeger
  • 3,270
  • 12
  • 28
1

Just export the generated codes into files in a temp directory, invoke javac in there, package them, serve them. Nothing fancy needed.

kay
  • 23,543
  • 10
  • 89
  • 128
  • This worked out just fine for me. Thanks for idea. I have uploaded the basic prototype on github (https://github.com/adeydas/Compilation-Unit) for reference. – Abhishek Dey Das Jun 17 '14 at 17:31