How do I install this damn thing?

Skip to end of metadata
Go to start of metadata

Packages of ANTLR available from the repositories of distributions like Ubuntu are quite old and probably worth avoiding.

Getting a proper Java virtual machine running

You can skip this step if you know Java is properly installed.

  1. Install a compatible Java virtual machine.
    • On Ubuntu or Debian Linux, you can install OpenJDK from the package manager:
      sudo apt-get install default-jdk
      
    • On other platforms, use either the Oracle/Sun distribution or OpenJDK.
  2. Check that your Java installation is working:
    java -version
    
    (tick) Expect output like the following (for OpenJDK on Ubuntu):
    java version "1.6.0_20"
    OpenJDK Runtime Environment (IcedTea6 1.9.5) (6b20-1.9.5-0ubuntu1~10.04.1)
    OpenJDK 64-Bit Server VM (build 19.0-b09, mixed mode)
    

Installing ANTLR

  1. Visit the download page and download the "Complete ANTLR x.y Java binaries jar" file.
    • For example, from a Linux shell, download ANTLR 3.3 to your home directory:
      cd ~
      wget http://www.antlr.org/download/antlr-3.3-complete.jar
      
  2. Add ANTLR to your CLASSPATH environmental variable and run it:
    export CLASSPATH=~/antlr-3.3-complete.jar:$CLASSPATH
    java org.antlr.Tool -version
    
    (tick) Expect output like the following:
    ANTLR Parser Generator  Version 3.3 Nov 30, 2010 12:50:56
    
    (error) If you see output like the following, the CLASSPATH is not set up properly:
    Exception in thread "main" java.lang.NoClassDefFoundError
    
    (error) If you see an older version of ANTLR, your CLASSPATH may not be set up properly and Java may be finding ANTLR in .jar files bundled with an application like BEA WebLogic. Ensure the path to the current .jar of ANTLR is at the beginning of your CLASSPATH.
  3. Configure your CLASSPATH to include ANTLR on future logins.
    • For example, on a BASH shell, add the environmental variable to the .bashrc script:
      echo "export CLASSPATH=~/antlr-3.3-complete.jar:$CLASSPATH" >> ~/.bashrc
      
  1. May 11, 2008

    I know I'm an idiot for asking this but I've just used C/C++ flex and bison for my parsing/lexing needs. I don't know anything about Java. I'm running Ubuntu 7.10 with the following java -version output:

    dave@dave-laptop:~/src/antlr-3.0.1$ java -version
    java version "1.4.2-02"
    Java(TM) 2 Runtime Environment, Standard Edition (build Blackdown-1.4.2-02)
    Java HotSpot(TM) Client VM (build Blackdown-1.4.2-02, mixed mode)

    How do I add all the jars in lib to my CLASSPATH ?

  2. May 11, 2008

    Hi David. Either from command line or in your .bash_profile, do something like:

    export CLASSPATH="$CLASSPATH:antlr.jar:antlr-2.7.7.jar:stringtemplate.jar"

    Naturally you need full path to those jar files in there and the exact jar names (might have version differences in names from what I have there).

  3. Dec 16, 2008

    Replaced by my 2008-12-16 post

  4. Dec 16, 2008

    Replaced by my 2008-12-16 post

  5. Dec 16, 2008

    Replacing my previous comments regarding confusion over downloads with an explanation which might be useful to others.

    ANTLR installation
    --------------------
    Attempting to follow along with the Definitive ANTLR Reference, I was having difficulty reconciling what it describes regarding installation, versus what's actually offered for download on this site.

    On page: http://antlr.org/download.html
    the most prominent offering is the ANTLRWorks+ANTLR download, which is described as "containing" ANTLR, suggesting that this download can be used not just for the ANTLRWorks GUI, but also for command-line operation of ANTLR.

    However, that download includes just antlrworks-1.2.2.jar, which is sufficiently different from what the book discusses that it's possibly confusing for non-Java-savvy readers.

    So, the trick is this: 

    1. Launching antlrworks-1.2.2.jar (eg: by double-clicking it in Windows) causes the ANTLRWorks GUI to launch. But wait! That's not all!

    2. Like all jars, antlrworks-1.2.2.jar contains a zipped directory structure, which (if the jar is included in the CLASSPATH) Java surveys to find Java classes. Turns out that antlrworks-1.2.2.jar includes all the same subdirectories and Java class files (ie: provides classes with the same fully-qualified class names) as the three jars mentioned by the book (antlr-3.x.jar, antlr-2.7.7.jar, and stringtemplate-3.x.jar).

    3. So, to get on track with the book, instead of the three jars the book mentions, add the path of antlrworks-1.2.2.jar to the CLASSPATH environment variable. This will allow the java environment to find the classes that the book refers to -- for example the org.antlr.Tool class.

    Book sample code download
    -----------------------------
    Especially if you bought the paper version, you might have difficulty finding out where to download the code samples that appear in the book:

    http://www.pragprog.com/titles/tpantlr/source_code

    Hope that helps.

  6. Dec 29, 2008

    Hello,Terence.There is something I do not understand.I have downloaded antlr-3.1.1 today.Since there are antlr-3.1.1.jar and antlr-2.7.7.jar in the directory antlr-3.1.1\lib , and both of them contain the file antlr\Tool.class . If they are all put into the Classpath, then which Tool.class will be executed when I use the following command: java antlr.Tool simple.g

  7. Dec 29, 2008

    Hi. Since 3.1 (or was it 3.1.1), antlr.jar has everything in it you need to run the tool and use the generated output. sorry for the confusion.