265

In which languages are the Java compiler (javac), the virtual machine (JVM) and the java starter written?

0

10 Answers 10

214

The precise phrasing of the question is slightly misleading: it is not "the JVM" or "the compiler" as there are multiple JVM vendors (jrockit is one, IBM another) and multiple compilers out there.

  • The Sun JVM is written in C, although this need not be the case - the JVM as it runs on your machine is a platform-dependent executable and hence could have been originally written in any language. For example, the original IBM JVM was written in Smalltalk

  • The Java libraries (java.lang, java.util etc, often referred to as the Java API) are themselves written in Java, although methods marked as native will have been written in C or C++.

  • I believe that the Java compiler provided by Sun is also written in Java. (Although again, there are multiple compilers out there)

9
  • 1
    Actually, one library in Sun's JRE is written in NetRexx. (It's one of the arbitrary precision math libraries, either BigInteger, BigNum or BigDecimal. I forgot which one.) In theory, you could use any language to implement the JRE, as long as it can compile to a representation that a Java program can understand. Jan 26, 2010 at 11:05
  • 21
    Please provide a reference to the Sun JVM being written in C. Apr 2, 2011 at 19:03
  • 10
    The HotSpot JVM is written in C++ - www2.research.att.com/~bs/applications.html
    – devdimi
    Mar 19, 2012 at 11:05
  • 5
    @devdimi the link is broken and now resides at: stroustrup.com/applications.html
    – flup
    Jul 13, 2013 at 11:25
  • 3
    Good luck with that, cyotee. What would your JVM run on? Mar 3, 2017 at 9:59
170

The very first Java compiler was developed by Sun Microsystems and was written in C using some libraries from C++. Today, the Java compiler is written in Java, while the JRE is written in C.

We can imagine how the Java compiler was written in Java like this:

The Java compiler is written as a Java program and then compiled with the Java compiler written in C(the first Java compiler). Thus we can use the newly compiled Java compiler(written in Java) to compile Java programs.

10
  • 81
    +1 for explaining how a Java compiler can be written in Java :) Jul 5, 2014 at 20:22
  • 4
    fyi this process of "upgrading" to new compilers by compiling their code in more basic compilers is called "bootstrapping", as in "pulling yourself up by your bootstraps", which is where "booting" a machine comes from. Computerphile on YouTube has a good video about this and "T diagrams"
    – iono
    Oct 9, 2019 at 6:42
  • 5
    Does that also answer the chicken and egg problem :)
    – Junaid
    Nov 6, 2019 at 9:55
  • 2
    In theory, you dont' even need the bootstraping compiler. You can refer to the Java Language Specification and manually translate the Java Compiler code into bytecode(class file), hence bootstrapping. Though I only tried manually do that with a HelloWorld. Dec 18, 2020 at 14:37
  • 2
    Just clarifying, the JVM cannot be written in Java because Java needs JVM to run so if JVM-version2 was written in Java, it would have to run on JVM-version1, thus it would be a VM running on a VM. Oct 8, 2021 at 9:21
79

From Java Docs

The compiler is written in Java and the runtime is written in ANSI C

7
  • 2
    Very first Java compiler developed by Sun Microsystems was written in C using some libraries from C++ en.wikipedia.org/wiki/Java_compiler
    – Rahul Garg
    Aug 3, 2009 at 7:00
  • 4
    This is really rather an old document (>10 years by the look of things). For example, it says "Java bytecodes are translated on the fly to native machine instructions (interpreted) and not stored anywhere" which has not been true for about 5 years! Aug 3, 2009 at 7:02
  • 3
    Well, since when has documentation been really up-to-date, especially after large changes? :)
    – Esko
    Aug 3, 2009 at 9:41
  • 10
    What compiles Java? Java. Well, how do you get Java? Compile it. Huh?
    – user4903
    Jun 17, 2011 at 17:30
  • 3
    @user4903 Not necessarily. You can translate Java code into bytecodes by hand if you would like to. Dec 18, 2020 at 14:38
20

Actually the Oracle JVM is written in C++, not C.

Take a look at the HotSpot JVM code here: http://openjdk.java.net/groups/hotspot/

11

In principle almost anything, usually C

10

This link should answer your question

It seems the compiler is now written in Java, but the runtime is written in ANSI C

0
8
  • When Java was introduced by Sun Microsystem, the java compiler was written in C using some libraries from C++.
  • As there is a concept in Compiler Design called Bootstrapping, Mostly it is used in Compiler Development, Bootstrapping is the process of writing a compiler(Or Assembler) In the source programming language which it is intended to compile. It is used to produce a self-hosting compiler. The development of compilers for new Programming languages first developed in an existing language and then rewritten in the new language and compiled by itself. That's why today, Java compiler is written in Java itself.
  • Java Virtual Machine: Java virtual machine is an abstract machine. Like a real computing machine, It has an instruction set and manipulates various memory areas of runtime. Usually, JVM interprets the byte code into Machine code.

(For More Information You can check this link: https://docs.oracle.com/javase/specs/jvms/se7/html/)

3
  • amazing. especially the second bullet point.
    – hadi
    Oct 23, 2021 at 13:49
  • 1
    The JVM might be a virtual machine that executes Java bytecode, but somewhere, it itself has to be coded in something.
    – user904963
    Dec 26, 2021 at 3:14
  • Yes that's why I mentioned the concept of Bootstratpping here, you can refer the article to understand the concept geeksforgeeks.org/bootstrapping-in-compiler-design Jul 27, 2023 at 20:46
3

Jikes RVM, a self-hosting JVM used extensively for research purposes is written in Java. It's not the one people run on their desktops, but it's way up there on my list of "now let's show off the general power of the language."

1
  • 1
    It's turtles all the way down? Does it have some kind of microkernel? If so, what language is that written in?
    – Raedwald
    Jul 21, 2013 at 23:19
3

Supposing you're talking about the Hotspot JVM, which is iirc provided by Sun, it is written in C++. For more info on the various virtual machines for Java, you can check this link. javac, like most Java compilers, is written in Java.

-1

As always popular languages written in - C, C++.

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.