0
votes

Following this post, I am using the following steps to compile the parser/lexer from this repository:

export CLASSPATH=".:/usr/local/Cellar/antlr/&ltversion&gt/antlr-&ltversion&gt-complete.jar:$CLASSPATH"
antlr &ltgrammarName&gt.g4 -o &ltsomeFolder&gt/
javac &ltsomeFolder&gt/&ltgrammarName&gt*.java

but when I use the instructions here:

grun <someFolder>/<grammarName> tokens -tokens < <inputFile>

I get this error messages:

Exception in thread "main" java.lang.NoClassDefFoundError: IllegalName: &ltsomeFolder&gt/&ltgrammarName&gtLexer
    at java.base/java.lang.ClassLoader.preDefineClass(ClassLoader.java:889)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1014)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:825)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:723)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:646)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:604)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at org.antlr.v4.gui.TestRig.process(TestRig.java:129)
    at org.antlr.v4.gui.TestRig.main(TestRig.java:119)

I would appreciate if you could help me know what is the problem and how I can resolve it.

1

1 Answers

1
votes

I don’t see where you’ve specified a package name, so now your Java classes are located in <someFolder>. Be sure to compile them in that folder.

Then you’ll need to add that folder to your classpath (probably instead of “.”)

Try adding <someFolder> into the CLASSPATH you’re exporting. Then leave it off of your grun command line.

Java will only load classes from the Classpath (it’s a security thing). When TestRig runs, it attempts to load your class by building the Java class name it would have produced for you Parser (and Java will have to find that class somewhere in the classpath).

Your could modify the grun alias to allow for you to specify a directory to search for your classes, and use the -cp option on the Java command, but that’s probably more trouble than just adding it to you classpath that you’re using for this testing.