Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 assumption wrong, and some of mentioned languages do not generate bytecode?)

like image 531
java.is.for.desktop Avatar asked Aug 10 '10 12:08

java.is.for.desktop


1 Answers

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.

like image 195
JUST MY correct OPINION Avatar answered Nov 15 '22 00:11

JUST MY correct OPINION