1
votes

Ive been trying to test a simple project written by some students. Im testing it on Ubuntu and the project requires me to use ANTLR and a Makefile. It has been a nightmare to find a configuration of makefile, files and folders that compiles and executes successfully.

So basically this is the folder/file setup:

Makefile
test.txt
laboratorio/
  lab02/
    Main.java
    Lab02.g

The contents of Main.java are fairly simple. They just read the grammar token by token:

package laboratorios.lab02;
import org.antlr.v4.runtime.*;

public class Main{
    public static void main(String[] args) throws Exception{
        try{
            Lab02 lexer = new Lab02(new ANTLRFileStream(args[0]));
            while (lexer.nextToken().getType() != Token.EOF);
        }catch(ArrayIndexOutOfBoundsException aiobe){
            System.err.println("Must provide a valid path to the filename with the tokens");
            System.exit(1);
        }catch(Exception e){
            System.err.println("Must provide a valid path to the filename with the tokens");
            System.exit(1);
        }
    }
}

The contents of Lab02.g (the grammar) are irrelevant for the problem, but it produces a Lab02.java file (with package laboratorios.lab02) that must be compiled and referenced by the Main.java file.

The problem came when trying to test the makefile on Ubuntu. With every configuration I tried I kept getting errors (when compiling or running the Main java file) like:

  • java.lang.NoClassDefFoundError: org/antlr/v4/runtime/CharStream
  • Could not find or load main class org.antlr.v4.Tool
  • Error: Could not find or load main class

Ive followed the exact steps of this tutorial to set up antlr on my pc: Getting Started with ANTLR v4

1

1 Answers

0
votes

After many unsuccessful attempts I arrived at this magical configuration that made the whole project work nicely.

Makefile:

make:  gramatica main

gramatica: laboratorios/lab02/Lab02.g
    java org.antlr.v4.Tool laboratorios/lab02/Lab02.g

main: laboratorios/lab02/Lab02.java laboratorios/lab02/Main.java
    javac laboratorios/lab02/Lab02.java laboratorios/lab02/Main.java

run: laboratorios/lab02/Main.class
    java -cp ${CLASSPATH}:Lab02.class laboratorios/lab02/Main test.txt

clean:
    rm -rf laboratorios/lab02/*.tokens
    rm -rf laboratorios/lab02/*.class
    rm -rf laboratorios/lab02/Lab02.java
    rm -rf laboratorios/lab02/*~

I required me to set the CLASSPATH in the bashrc like this:

export CLASSPATH=.:~/your/path/to/antlr/antlr-4.2-complete.jar

As you would find in the tutorial I previously mentioned (Getting Started with ANTLR v4), it says you should write the Classpath using quotes. COMPLETELY IGNORE THAT ADVICE! Whenever I used quotes the main class wouldnt run as ANTLR was 'missing'. Also, the aliases in the tutorial are completely optional.

Some other things worth pointing out are that placing the Makefile inside the laboratorio/lab02/ folder was not helping either. Moving it out and referencing the java files from the outside helped A LOT. And the characters ".:" helped to execute the Main class as it would say it couldn't been found.