I am using ANTLR for a Compilers course, and working on a windows box. Getting ANTLR to work is not difficult since it is just a Java jar file. The quick start instructions on the ANTLR website seem really straightforward, and they probably are if you know what you are doing. However, I had a bit of trouble getting the handy antlr4
and grun
commands to work properly in my environment. This is likely due to my Unix/Linux background and because I am not used to messing with environment variables and batch files on windows. Adding things to the PATH and creating bash aliases in Linux is a piece of cake. This is a bit trickier on Windows so I figured I would make a short tutorial to help anyone else who does not know what they are doing like me.
Download Antlr
You can get the latest ANTLR from here. Save this file to C:\Javalib
Add ANTLR to the CLASSPATH
CLASSPATH is an environmental variable. In order to set an environmental variable in Windows you will need to do the following.
- Open Up File Explorer
- Right Click on This PC
- Select Properties
- On the left hand side of the new window select Advanced system settings
- On the bottom of this screen select Environment Variables…
- This is a new computer with no CLASSPATH variable so I had to add a new one. If you have used other Java Libraries it is possible that you may already have CLASSPATH defined. If you see CLASSPATH under System Variables append it with
C:\Javalib\antlr-4.5.3-complete.jar;
If you do not have a CLASSPATH variable defined, select **New…* and then enterCLASSPATH
for the Variable Name, andC:\Javalib\antlr-4.5.3-complete.jar
for the Variable value.
Create batch commands for ANTLR Tool and TestRig
In order to make ANTLR easier to run from the command line we will need to add two simple batch files somewhere in the PATH. I decided to put these in C:\Javalib in order to keep them organized and together. C:\Javalib is not in the PATH by default so we must append the existing PATH variable just like we did in the previous step.
- Open Up File Explorer
- Right Click on This PC
- Select Properties
- On the left hand side of the new window select Advanced system settings
- On the bottom of this screen select Environment Variables…
- Find the PATH variable
- Append
;C:\Javalib\;
to the existing PATH variable
Now we need to create the batch files.
- Create a new file in
C:\Javalib
calledantlr4.bat
- In this file enter
java org.antlr.v4.Tool %*
- Save this file
- Create a new file in
C:\Javalib
calledgrun.bat
- In this file enter
java org.antlr.v4.runtime.misc.TestRig %*
- Save this file
This will now allow us to enter antrl4
and grun
into a command prompt and launch ANTLR or the Testing Suite respectively.
Make Sure Everything Works
To make sure that all of this worked;
- Open up a Command Prompt or Powershell
- Enter the command
antlr4
- You should see something like this:

- Enter the command
grun
- You should see somethign like this:

Congrats, you are ready to hack on compilers now. Next, check out The Definitive ANTLR Reference book to learn everything there is to know about ANTLR.
Thank you. It was helpful. Do you’ve tutorials to run a sample code to get the output?
Thank you for the comment. I don’t have any sample code right now, but I would highly suggest looking in to The Definitive ANTLR Reference book. This is the book that I used when I was going through my CS program and it provided me with everything that I needed to know in order to use ANTLR.
you can visit antlr.com and you will find a simple example.
Thank you for your wonderful tutorial it is very helpful. I was looking for videos to do the installation but your tutorial was very easy.
Thanks for the comment, I am glad you found this helpful!
after setting all path variables in windows 7. i am getting error in cmd as antlr not recognized as internal or external command please help
I am getting the error:
‘java’ is not recognized as an internal or external command,
operable program or batch file.
This sounds like you don’t have Java installed. Did you install Java first? https://www.oracle.com/java/technologies/javase-jdk14-downloads.html