Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't java have a non-bytecode compiler? [duplicate]

Possible Duplicate:
Why isn't more Java software compiled natively?

I know that Java is byte code compiled, but when using the JIT, it will compile the 'hotspots' to native code. Why is there not an option to compile a program to native code?

like image 922
kmdent Avatar asked Dec 27 '22 11:12

kmdent


2 Answers

There is an option to compile Java source code files in binary code it is called GCJ and come from Free Software Foundation.

It is not officially supported by Sun/Oracle but i've used the compiler and it do a great job ;)

like image 171
aleroot Avatar answered Jan 16 '23 10:01

aleroot


Java, as a language, can be implemented, like any language, in many ways. This include full interpretation, bytecode compilation, and native compilation. See Java language specification

The VM specification defines how bytecode should be loaded and executed. It defines the compiled class format, and the execution semantics, e.g. threading issue, and what is called the "memory model". See Java VM specification

While orignally meant to go together, the language and the VM are distinct specifications. You can compile another language to Java bytecode and run it on top of the JVM. And you can implement the Java language in different way, notably without a VM, as long as you follow the expected semantics.

Of course, both are still related, and certain aspects of Java might be hard to support without bytecode interpretation at all, e.g. custom ClassLoader that return bytecode (see ClassLoader.defineClass). I guess the bytecode would need to be JIT'ed immediatly into native code, or maybe are not supported at all.

like image 30
ewernli Avatar answered Jan 16 '23 11:01

ewernli