antlr4-Can't load Hello as lexer or parser

爱⌒轻易说出口 提交于 2019-11-30 04:39:06

You generated Java source files by running ANTLR on the grammar, but TestRig is looking for compiled .class files. You need to run the Java compiler on the source files before using TestRig.

I had exactly the same error on Windows. My batch is set like this at first:

java -cp "C:\ANTLR\antlr-4.4-complete.jar" org.antlr.v4.runtime.misc.TestRig %*

It turns out all my compiled Hello*.class files are located in current folder and I forget to include the current folder in the -cp. After I change to below. It works. Note the leading dot in the -cp.

java -cp ".;C:\ANTLR\antlr-4.4-complete.jar" org.antlr.v4.runtime.misc.TestRig %*

After all of your *.java files have been generated, and your *.class files have been generated, and your %CLASSPATH% is set correctly, based on the "Hello World" example grammar, you should not see this error.

If you don't have the "antlr-runtime-4.4.jar" in your %CLASSPATH% environment variable and run "grun" code from the book, you will receive this error when you run your generated parser based on the grammar you supplied.

%CLASSPATH% should be:

C:\libraries\antlr-4.4-complete.jar;C:\libraries\antlr-runtime-4.4.jar;

Enter the input after this command is entered. Like "hello parrt" in the book example. Then enter CTRL+Z (in Windows) to end the input, or CTRL+D (on Unix).

In order to do this correctly, I'm going to assume that you've got a JDK installed and added to your system PATH variable.

In this case you will need two batch files to help you out.

antlr4.bat:

java -cp antlr-4.2-complete.jar org.antlr.v4.Tool %*

grun.bat:

java -cp .;antlr-4.2-complete.jar org.antlr.v4.runtime.misc.TestRig %*

This also assumes that you have the antlr-4.2-complete.jar in the current directory. Beyond that simply follow the instructions in the getting started guide.

Note that I used the -cp flag because, on Windows 7, I'm not seeing a CLASSPATH variable.

James Lynch

You need Antlr jar file and the .bat files in your CLASSPATH. I know because I had the exact same problem.

Windows

Go to the start menu and search for Environment Variables and you will find Set Environment Variables for Users or something like that. There should be a section labeled Variables for all Users. If you see %CLASSPATH%, click it, then click the edit button under that box. Go to the very end of the list and add C:\**PATH TO JAR FILE**\antlr-X.X-complete.jar; to the end. Repeat that with the path to each of the .bat files. If you don't see %CLASSPATH%, then under the box, click the button that says add, and add %CLASSPATH%, and do the same process.

OSX

I am not as familiar with OSX and the storage system, but you can do export CLASSPATH="/Users/**USER**/**PATH TO JAR FILE**/antlr-X.X-complete.jar:$CLASSPATH" in the terminal. Repeat, but with the paths to each of the .bat files. Simple as that hopefully. Let me know if this is wrong, because like I said above, OSX is not my strong point. You could also check out this question on StackOverflow: How can I permanently add a class path to my Mac terminal?

I added OSX instructions just in case anyone else had this problem, but on OSX! :)

ALSO: Replace the X's in antlr-X.X-complete.jar with the version number, or just make it the name of the jar file. Whatever floats your boat.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!