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

查看:38
本文介绍了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.batgrun.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-ing 进入编译文件所在的文件夹(而不是从根项目文件夹调用 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.


据我所知,grunantlr4-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天全站免登陆