Short Question - About javac.c where is it?

David Buck david.buck at oracle.com
Sun Jul 22 02:26:09 UTC 2018


Hi Max!

The compiler that compiles Java source code into Java bytecode is 
written entirely in Java itself. The only C code involved is the 
launcher executable, "javac" which (as explained by Erik) is simply a 
very small piece of JNI code that starts the JVM and points it to the 
main class. Everything else is pure Java.

If you are really interesting in learning how the Java compiler works, I 
would recommend running it in a debugger. You can write a small stub 
that calls it via JavaCompiler [0] and then you don't need to worry 
about the native code used to invoke javac from the command line. If you 
are new to compilers in general, I would recommend reading "The Tiger 
Book" [1]. (Much of the book's content, especially many of the later 
chapters, would not directly apply to javac, but it is still a great 
book if you are looking for a place to start.)

There are also a ton of great resources linked from the compiler team's 
OpenJDK project page [2].

Cheers,
-Buck

[0] https://docs.oracle.com/javase/8/docs/api/javax/tools/JavaCompiler.html

[1] https://www.cs.princeton.edu/~appel/modern/java/

[2] http://openjdk.java.net/groups/compiler/


On 2018/07/21 9:56, mr rupplin wrote:
> Hello.
> 
> I look and find that we start here:
> 
> http://hg.openjdk.java.net/jdk/jdk/file/b0fcf59be391/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Main.java
> 
> public Result compile(String[] argv, Context context)
> 
> and this leads to:
> 
> http://hg.openjdk.java.net/jdk/jdk/file/b0fcf59be391/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java
> 
> comp.compile(args.getFileObjects(), args.getClassNames(), null, List.nil());
> 
> 
> Which leads to genCode:
> 
> http://hg.openjdk.java.net/jdk/jdk/file/b0fcf59be391/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java
> 
>   JavaFileObject genCode(Env<AttrContext> env, JCClassDecl cdef) throws IOException
> 
> Which calls:
> 
> 
> return writer.writeClass(cdef.sym);
> 
> And so I think if I've understood you (without a debugger running on this end) the JavaCompiler (in Java) will do a C compile on various C source code documents and share libraries and this will make the native binary "javac".  Is that correct?
> 
> The file "javac" is a native binary that is produced with a JavaCompiler called from the "bash configure && make all" command?
> 
> Thanks!
> 
> 😊 MR/
> ________________________________
> From: Erik Joelsson <erik.joelsson at oracle.com>
> Sent: Friday, July 20, 2018 6:56 PM
> To: mr rupplin; build-dev at openjdk.java.net
> Subject: Re: Short Question - About javac.c where is it?
> 
> Hello Max,
> 
> Javac, like most of the command line tools in the JDK is written in
> Java, so to look at the actual program, I would recommend starting here
> (assuming current mainline at http://hg.openjdk.java.net/jdk/jdk):
> 
> src/jdk.compiler/share/classes/com/sun/tools/javac/Main.java
> 
> Most of the executables in the JDK are built from the same launcher src
> file with some different -D macro flags set to control which java class
> gets invoked by that launcher. The source for the launchers is located here:
> 
> src/java.base/share/native/launcher/main.c
> 
> Does that answer your question?
> 
> /Erik
> 
> 
> On 2018-07-20 15:31, mr rupplin wrote:
>> In building the OpenJDK where may we find the source code file(s) for the javac output?
>>
>> In libjli we find the java.c but locate javac.c returns no results.  How curious.
>>
>> Would you be a friend and help us out.  We love reading.
>>
>> Thanks,
>>
>> Max
>>
>> /sr software developer
> 



More information about the build-dev mailing list