| 1 |
# Installation |
| 2 |
|
| 3 |
Please read carefully: [Getting Started with ANTLR v4](https://raw.githubusercontent.com/antlr/antlr4/master/doc/getting-started.md). |
| 4 |
|
| 5 |
## Why can't ANTLR (grun) find my lexer or parser? |
| 6 |
|
| 7 |
If you see "Can't load Hello as lexer or parser", it's because you don't have '.' (current directory) in your CLASSPATH. |
| 8 |
|
| 9 |
```bash |
| 10 |
$ alias antlr4='java -jar /usr/local/lib/antlr-4.2.2-complete.jar' |
| 11 |
$ alias grun='java org.antlr.v4.runtime.misc.TestRig' |
| 12 |
$ export CLASSPATH="/usr/local/lib/antlr-4.2.2-complete.jar" |
| 13 |
$ antlr4 Hello.g4 |
| 14 |
$ javac Hello*.java |
| 15 |
$ grun Hello r -tree |
| 16 |
Can't load Hello as lexer or parser |
| 17 |
$ |
| 18 |
``` |
| 19 |
|
| 20 |
For mac/linux, use: |
| 21 |
|
| 22 |
```bash |
| 23 |
export CLASSPATH=".:/usr/local/lib/antlr-4.2.2-complete.jar:$CLASSPATH" |
| 24 |
``` |
| 25 |
|
| 26 |
or for Windows: |
| 27 |
|
| 28 |
``` |
| 29 |
SET CLASSPATH=.;C:\Javalib\antlr4-complete.jar;%CLASSPATH% |
| 30 |
``` |
| 31 |
|
| 32 |
**See the dot at the beginning?** It's critical. |
| 33 |
|
| 34 |
## Why can't I run the ANTLR tool? |
| 35 |
|
| 36 |
If you get a no class definition found error, you are missing the ANTLR jar in your `CLASSPATH` (or you might only have the runtime jar): |
| 37 |
|
| 38 |
```bash |
| 39 |
/tmp $ java org.antlr.v4.Tool Hello.g4 |
| 40 |
Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/v4/Tool |
| 41 |
Caused by: java.lang.ClassNotFoundException: org.antlr.v4.Tool |
| 42 |
at java.net.URLClassLoader$1.run(URLClassLoader.java:202) |
| 43 |
at java.security.AccessController.doPrivileged(Native Method) |
| 44 |
at java.net.URLClassLoader.findClass(URLClassLoader.java:190) |
| 45 |
at java.lang.ClassLoader.loadClass(ClassLoader.java:306) |
| 46 |
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) |
| 47 |
at java.lang.ClassLoader.loadClass(ClassLoader.java:247) |
| 48 |
``` |
| 49 |
|
| 50 |
## Why doesn't my parser compile? |
| 51 |
|
| 52 |
If you see these kinds of errors, it's because you don't have the runtime or complete ANTLR library in your CLASSPATH. |
| 53 |
|
| 54 |
```bash |
| 55 |
/tmp $ javac Hello*.java |
| 56 |
HelloBaseListener.java:3: package org.antlr.v4.runtime does not exist |
| 57 |
import org.antlr.v4.runtime.ParserRuleContext; |
| 58 |
^ |
| 59 |
... |
| 60 |
``` |