ANTLR Introduction to ANTLR v4 Install in Eclipse and Build Hello World


(Tested with ANTLR 4.5.3, Eclipse Neon, ANTLR 4 IDE 0.3.5, and Java 1.8)

  1. Download the latest ANTLR. Make sure to get the complete ANTLR Java binaries jar. Save to any appropriate location, for example the folder where other Java libraries are stored. It doesn’t matter where, just remember the location.
  1. Install the ANTLR IDE in Eclipse.

    • From the Eclipse menu, click Help and select Eclipse Marketplace.
    • In the Find: box, type antlr and click Go.
    • Click Install for ANTLR 4 IDE.
    • Click Finish in the Confirm Selected Features window.
    • If a Security Warning window pops up, click OK.
    • Restart Eclipse.
  2. Work around for the “Failed to create injector…” error.

    • When accessing ANTLR 4 Preferences in Eclipse or when the environment variable HOME is not set, the following error occurs: Failed to create injector for com.github.jknack.antlr-4ide.Antlr4 for com.github.jknack.antlr-4ide.Antlr4.
    • Make sure the environment variable HOME is set. If not, set it as appropriate for your system.
    • Download Xtext 2.7.3 to the same location as antlr-n.n.n-complete.jar.
    • In Eclipse, click on Help and select Install New Software.
    • Click Add… to get to the Add Repository window.
    • Type a name, xtext 2.7.3 for example, then click on Archive…, navigate to the Xtext 2.7.3 file and select it, then click OK.
    • In the Install window, click the Select All button then click Next> twice, accept the license agreement. and click Finish.
    • Restart Eclipse.
  3. Tell Eclipse/Java where ANTLR is.

    • In Eclipse, click on Window and select Preferences.
    • In the left pane, expand Java and Build Path, then select Classpath Variables.
    • In the right pane, click New…, enter a Name, and click File… and browse to your location of antlr-n.n.n-complete.jar. Click OK to get back to the Classpath Variables window.
    • Click OK to exit Preferences.
  4. (Optional) Configure the ANTLR IDE generated sources directory.

    • In the Eclipse main window, click Window then Preferences.
    • In the left pane, expand ANTLR 4 and select Tool.
    • Under Options, change the directory if desired. For example, java is my target language so I use ./antlr-java.
    • Click OK to close the Preferences window.
  5. Create an ANTLR 4 project.

    • From the Eclipse main window, go to File, New, Project.
    • In the New Project window, expand General and select ANTLR 4 Project.
    • Click Next, type a project name and click Finish.
    • The default new project contains a Hello.g4 file and will automatically build the standard "Hello World" program.
    • In the Package Explorer, expand the new project folder to see the g4 file and a folder named target (or the name you gave it in Step 5) containing the target source files.