1

So I got to know that javac compiler is written in Java. And JVM converts the bytecode (compiled Java code) to machine code for execution.

Two questions:

  1. If javac is written in Java, does compiling Java source code require the JVM to start?
  2. If javac is written in Java, what compiler compiled javac?

I'm assuming the initial javac was written in C and the modern Java compilers are compiled using the primordial javac (written in c).

3
  • 1
    This question is similar to: In which language are the Java compiler and JVM written?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 1 at 19:51
  • 1
    Once they had a Java compiler written in Java there would be no great incentive to maintain a C or other non Java language one side by side. Commented Jul 1 at 20:01
  • 1
    You should read "Reflections on trusting truth" by Ken Thompson, about the danger of bootstrapping languages compilers (1st compiler is written in assembly language. Then subsequent version of the compilers are written in the language they compile, each of them being compiled with an older version) :D Commented Jul 1 at 21:09

1 Answer 1

5

If javac is written in java, does compiling a java code require the JVM to start?

Yes, running javac requires a JVM or some equivalent way to execute Java code. (You could, I suppose, precompile it as a native image with GraalVM or the like.)

If javac is written in java, what compiler compiled javac?

There might have been a primordial C compiler long ago, but it would only have been needed at the very beginning, and it certainly wouldn't be needed anymore. These days, the previous version of javac can compile the next version of javac.

Sign up to request clarification or add additional context in comments.

3 Comments

It does not start the java executable itself: strace -e fork,vfork,clone,execve -f -b execve javac Test.java 2>&1 | grep -Ev 'attached|exited|SIG' execve("/usr/bin/javac", ["javac", "Test.java"], 0x7ffdd8305b80 /* 115 vars */) = 0
@LMC javac may not execute the java tool directly, but it still creates a JVM. My guess is via the JNI_CreateJavaVM JNI function (or some internal equivalent). Though I don't understand how openjdk/jdk creates the native tool executables well enough to point at the exact code responsible for creating the JVM.
I think, there’s a single template for all the JDK tools, as they only differ in which class to load to invoke its main method. It could even work with a single binary for all of them, choosing a class name based on the binary’s file name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.