开发者

How do languages/runtimes based on JVM generate Java bytecode?

There are several languages/runtimes atop the JVM: such as JRuby, Groovy, Jython, Clojure, Rhino, Scala, and so on...

  • How do these generate Java bytecode?
  • Are there standardized libraries for doing this?
  • How the generated bytecode gets executed?

(Or is my assumptio开发者_JAVA百科n wrong, and some of mentioned languages do not generate bytecode?)


Some of them generate bytecode. Some have a runtime system that executes as an interpreter. Some have a blend of the two. The JVM is, after all, a CPU (which just happens to not really exist) with a certain architecture and a certain set of instructions. You target it with your code generator the same way you generate object code for any CPU. (Now to be fair its instruction set is very heavily focused on Java's capabilities and needs, but it's still general enough to support other models. Barely.)

There are a variety of ways to generate JVM byte-code. You can handroll your own .class file generation routines (since the format and the instruction set is well-specified). You can use a number of libraries like ASM or BCEL (which is the most common approach, I think). Or you could generate Java code as your intermediate representation and then compile that with the usual Java tools.

You execute the code the same way you execute Java-compiled code: the Java runtime loads the generated .class files and starts running. This is all part of the specification as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜