0

I am trying to run ANTLR v4 with Java. I am trying to run the below file, Hello.java:

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTree;

public class Hello {
    public static void main(String[] args) throws Exception {
        System.out.println("Testing");
    }
}

with the following script test.sh:

#!/bin/sh
ANTLR_LIB="antlr-4.9.2-complete.jar"

# Making an empty out directory
touch out
rm -r out
mkdir out

# Generating code
java -jar ${ANTLR_LIB} -no-listener -visitor Expr.g4 -o out/


# Adding in our own code
cp *.java out/

# Compiling everything together
javac -classpath ".;[MY FILE PATH]\antlr-4.9.2-complete.jar" -cp .:out:${ANTLR_LIB} out/Hello.java

# Showing the parse tree
# java -cp .:out:${ANTLR_LIB} org.antlr.v4.gui.TestRig Expr prog -gui $1

# Run the calculator
# java -classpath .:out/:${ANTLR_LIB} Calc $1


I have set my CLASSPATH to include antlr-4.9.2-complete.jar, but when I try to run the script, I run into this error:

enter image description here

I am unsure how to resolve this error.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Alex Andra
  • 31
  • 1
  • 2
  • Your javac command has `-cp` and `-classpath` which is odd (I don't know what the result of doing that is). Where exactly is `antlr-4.9.2-complete.jar`? – tgdavies Oct 30 '21 at 06:18
  • @tgdavies antlr-4.9.2-complete.jar and Hello.java are stored in the same directory: $ dir Hello.java antlr-4.9.2-complete.jar Hello.java antlr-4.9.2-complete.jar – Alex Andra Oct 30 '21 at 06:26

1 Answers1

0

As mentioned in the comments, you're using both -classpath and -cp: don't. Just use one of them.

Compiling all Java source files inside ./out (of which some are dependent on the ANTLR runtime) can be done as follows:

Linux & Mac OS

javac -cp .:out:antlr-4.9.2-complete.jar out/*.java

Windows

javac -cp .;out;antlr-4.9.2-complete.jar out\*.java
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288