Building OpenJDK
If you are looking for updated instructions on how to build the OpenJDK JDK Project, please see https://openjdk.org/groups/build/doc/building.html.
The rest of this page describes how the
compiler-grammar
project was built in JDK 7.
Building the Project
The project is in a standard set of OpenJDK repositories. You
can either clone the entire forest, and build a complete standalone
version of OpenJDK containing the new compiler, or you can just
clone and build the langtools
to get a copy of
javac.
In addition to all the normal dependencies for OpenJDK, to build
the langtools
repository, you need to download ANTLR
3.1.1 or later. There are two different bundles you can use.
For more details and other versions, see the ANTLR download page. Note that the version number is currently specified in the ANTLR jar file name, so if you use a newer version, you will have to update the langtools make/build.xml file accordingly.
When you build the langtools repository, you must set the
variable antlr.home
to a directory that contains
lib/antlr-3.1.1.jar
. If you download the full ANTLR
source distribution, you can simply set antlr.home
to
the location where you unpack the bundle. If you just download the
ANTLR 3.1.1 jar file, you must set up a directory to contain
lib/antlr-3.1.1.jar
and set antlr.home
to
point to that directory.
Example
Assuming you have suitable copies of JDK and ANTLR 3.1.1
installed, the following commands will download and build the
compiler-grammar/langtools
repository.
% cd work % hg clone http://hg.openjdk.org/compiler-grammar/compiler-grammar/langtools % cd langtools % ant -f make/build.xml \ -Dboot.java.home=/opt/jdk/1.6.0 -Dantlr.home=/opt/antlr/3.1.1 build-javac
Once you have built javac in the langtools repository, the
dist
directory will contain a copy of the javac
compiler in dist/lib/javac.jar
. You can use this jar
file in a number of ways.
-
Execute it directly in the standard way.
% java -jar dist/lib/javac.jar ...
-
Use the simple wrapper script provided in
dist/bin/javac
.% dist/bin/javac ...
-
Put the jar file on the bootclasspath used by the standard javac launcher.
% javac -J-Xbootclasspath/p:dist/lib/javac.jar ...
Note the use of -J to set the bootclasspath used to locate the classes for javac. (By itself, -Xbootclasspath is a javac option used to locate the runtime classes used when compiling your program.)
Tool showing grammatical structure of Java code
Check out this simple tool for showing how a Java source file is parsed. ( Screenshot. )