3
<dependencies>
    <dependency org="org.apache.commons" name="commons-math3" rev="3.2"/>
    <dependency org="org.antlr" name="antlr4" rev="4.0"/>
    <dependency org="org.antlr" name="antlr4-runtime" rev="4.0"/>
    <dependency org="org.antlr" name="antlr4-master" rev="4.0"/>
</dependencies>

我使用 Ivy 使用 ant 脚本将上面的依赖 jar 下载到 lib 目录中。 antlr4-4.0.jar是这些依赖项之一。但是当我在终端中这样做时:

 java -cp antlr4-4.0.jar org.antlr.v4.Tool

我得到这个例外:

Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/v4/runtime     /misc/ParseCancellationException
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:787)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:447)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2451)
    at java.lang.Class.getMethod0(Class.java:2694)
    at java.lang.Class.getMethod(Class.java:1622)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.antlr.v4.runtime.misc.ParseCancellationException
    ... 25 more

我究竟做错了什么?

4

2 回答 2

2

感谢earcam,我通过将所有依赖项添加到由以下分隔的类路径中解决了我所做的问题:

java -cp lib/antlr4-4.0.jar:lib/antlr4-runtime-4.0.jar:lib/stringtemplate-3.2.1.jar:lib/ST4-4.0.7.jar:lib/antlr-runtime-3.5.jar org.antlr.v4.Tool
于 2013-06-03T08:59:56.057 回答
0

另一种选择是使用 ivy 作为独立 jar来启动 antl4r:

$ java -jar ivy-2.3.0.jar -dependency org.antlr antlr4 4.0 -confs default -warn -main org.antlr.v4.Tool
ANTLR Parser Generator  Version 4.0
 -o ___              specify output directory where all output is generated
 -lib ___            specify location of grammars, tokens files
 -atn                generate rule augmented transition network diagrams
 -encoding ___       specify grammar file encoding; e.g., euc-jp
 -message-format ___ specify output style for messages in antlr, gnu, vs2005
 -long-messages      show exception details when available for errors and warnings
 -listener           generate parse tree listener (default)
 -no-listener        don't generate parse tree listener
 -visitor            generate parse tree visitor
 -no-visitor         don't generate parse tree visitor (default)
 -package ___        specify a package/namespace for the generated code
 -depend             generate file dependencies
 -D<option>=value    set/override a grammar-level option
 -Werror             treat warnings as errors
 -XdbgST             launch StringTemplate visualizer on generated code
 -XdbgSTWait         wait for STViz to close before continuing
 -Xforce-atn         use the ATN simulator for all predictions
 -Xlog               dump lots of logging info to antlr-timestamp.log

创建一个别名,您就有了自动启动 antlr4 的机制:

alias antlr4='java -jar ivy-2.3.0.jar -dependency org.antlr antlr4 4.0 -confs default -warn -main org.antlr.v4.Tool -args'

这种方法的唯一缺点是传递选项效果不佳:-(

antlr4 Hello.g4 -o test
于 2013-06-03T09:35:41.897 回答