SPL_Splinter
SPL_Splinter

Reputation: 503

How to import a custom java class to my Antlr Grammar?

In a directory called Test I have a java class in a file Word.java like this:

public class Word {
  public String value;

  public Word(String v) {
    value = v;
  }
}

And in the same directory my antlr4 grammar Test.g4 like this:

grammar Test;    
@header {
  import Test.Word;
}
@members {
  ArrayList<Word> words = new ArrayList<Word>();
}

program
@after { System.out.println(words.toString()); }
  : (ID { words.add(new Word($ID.text)); } )+
  ;

(In this example I'm just using Word instead of String to test the import thing. The grammar and the actions are not my problem yet)

Now, when I try to run java org.antlr.v4.runtime.misc.TestRig Test this is the outcome:

blablabla

Caused by: java.lang.ClassNotFoundException: Test.Word

blablabla

This is driving me crazy, how can I import my class in the .g4 file? I know that default java packages can be load but how do I import my custom java class?

tl;dr: I created a java class and want to use it inside my antlr grammar. How can I do that?

Upvotes: 5

Views: 2669

Answers (1)

Terence Parr
Terence Parr

Reputation: 5962

Your CLASSPATH will have to include dir containing Test.

Upvotes: 2

Related Questions