[antlr-interest] Import a class or a jar file in ANTLRWorks

Giampaolo Tomassoni Giampaolo at Tomassoni.biz
Wed Apr 28 05:11:35 PDT 2010


> Hello,
> In the last days i search everywhere for an answer to this:
> How can i import a class or a jar file in my .g file in ANTLRWorks.
> I se that you can import thinks like:
> 
> @header {
> import java.util.HashMap;
> }
> 
> but i can't import classes that i created in other project, or jar
> files of that project.
> I tried everythink, for example:
> 
> @header {
> import mypackage.ClassName;
> }
> and i put this jar in the same location whit the .g file and the
> generated files, but it dosen't work.

The "import" keyword in Java doesn't import the content of a jar: it only
makes the mypackage.ClassName available in your code as simply "ClassName",
instead of the full package path. It is more or less like a shortcut
declaration, then.

With respect to the ANTLR compiler, the content of @header{} definitions are
copied more or less verbatim to the generated parser/lexer java source, so
that your .jar file is not needed at all during grammar compilation.

Of course you need it to later compile the parser/lexer java source (with
javac or whatever you use for it) and to execute the parser/lexer classes.

So, you have to configure your development platform / java compiler as well
as your JVM in order to use the classes defined in a .jar, not the ANTLR
compiler.

Giampaolo



More information about the antlr-interest mailing list