ANTLR4 TestRig(grun)引发java.lang.NoClassDefFoundError异常 [英] ANTLR4 TestRig (grun) throws java.lang.NoClassDefFoundError exception

查看:169
本文介绍了ANTLR4 TestRig(grun)引发java.lang.NoClassDefFoundError异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习ANTLR 4,并且正在遵循权威ANTLR 4参考中给出的示例.可悲的是,我陷入了第一个例子.

I'm trying to learn ANTLR 4, and I'm following the examples given in The Definitive ANTLR 4 Reference. Sadly I'm stuck at the first example.

系统:
Windows 10(1703)
Java 8,更新151
ANTLR 4,v4.7

System:
Windows 10 (1703)
Java 8, update 151
ANTLR 4, v4.7

我的%CLASSPATH%设置为系统变量( .;"D:\ Program Files \ Java \ libs \ antlr-4.7-complete.jar"; ).
我的%PATH%中有 antlr4.bat grun.bat ,并且能够运行 antlr4 .

My %CLASSPATH% is set as a system variable (.;"D:\Program Files\Java\libs\antlr-4.7-complete.jar";).
I have antlr4.bat and grun.bat available in my %PATH%, and I'm able to run antlr4 from the command line.

antlr4.bat : java -cp%CLASSPATH%org.antlr.v4.Tool%*
grun.bat : java -cp%CLASSPATH%org.antlr.v4.gui.TestRig%*

我之所以明确使用 -cp%CLASSPATH%是因为我看到其他一些人在不使用 -cp 选项时遇到了问题.

I'm using -cp %CLASSPATH% explicitly because I've seen a few others having problems when not using the -cp option.

我的问题是这样的:
当我在示例语法 Hello ( grun Hello r -tokens )上运行 grun 时,我收到此错误消息

My problem is this:
When I run grun on the example grammar Hello (grun Hello r -tokens) I get this error message

Exception in thread "main" java.lang.NoClassDefFoundError: HelloLexer (wrong name: main/resource/hello/HelloLexer)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at org.antlr.v4.gui.TestRig.process(TestRig.java:129)
        at org.antlr.v4.gui.TestRig.main(TestRig.java:119)

对我在做什么错有任何想法吗?
提前非常感谢!

Any ideas to what I'm doing wrong?
Many thanks in advance!


我以为我找到了答案.通过 cd -进入编译文件所在的文件夹(而不是从根项目文件夹中调用 grun ),我设法了解了有问题的示例.


I thought I had found an answer. By cd-ing into the folder where the compiled files were (instead of calling grun from the root project folder) I managed to get through the example in question.

但是当我尝试建立自己的项目时,遇到了相同的问题,并带有与上面相同的错误消息.

But when I tried to set up my own project, I came across the same issue, with the same error message as above.

这一次,当我使用 -package 选项运行 antlr4 后,似乎 grun 抱怨了.

This time, it seems like grun is complaining after I've run antlr4 with the -package option.

有人知道这是为什么吗?

Does anyone know why this is?

推荐答案

哇...该解决方案当然是一个简单的解决方案...我的问题是我有 cd 转到测试项目的根文件夹,然后只需要 cd 到编译文件所在的目录(并从其中调用 grun )即可.

Wow... The solution was, of course, an easy solution... My problem was that I had cd'd to the root folder of my test project, and I just had to cd to (and call grun from) the directory where the compiled files are.


看来我仍然对 grun 有问题.请参阅已编辑的问题.


It seems like I still have an issue with grun. See edited question.

Edit2:
据我所知, grun antlr4 -package 选项配合不佳.
因此,我的解决方案是必须将我的项目设置为Gradle项目,因为显然效果更好.


From what I can tell, grun doesn't cooperate very well with antlr4's -package option.
So my solution was that I had to set up my project as a Gradle project, because apparently that works better.

这篇关于ANTLR4 TestRig(grun)引发java.lang.NoClassDefFoundError异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆