对于ANTLR类,Maven测试失败,NoClassDefFoundError

时间:2014-12-09 11:21:21

标签: java maven antlr

我正在使用main/resources/antlr-4.2-complete.jar外部库。我已将它包含在我的类路径中。但是当我运行测试目标时,在Eclipse中我收到以下消息

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.048 sec <<< FAILURE!
checkGrammar(br.com.stoneage.GrammarTest)  Time elapsed: 0.011 sec  <<< ERROR!
java.lang.NoClassDefFoundError: org/antlr/v4/runtime/CharStream

所以我知道测试目标不是寻找antlr-4.2-complete.jar。我该如何解决这个问题?

这是我的POM文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>br.com.stoneage</groupId>
  <artifactId>SASInterpreter</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SASInterpreter</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

   </dependencies>
</project>

1 个答案:

答案 0 :(得分:1)

根据您的意见,我强烈建议您在POM中声明对ANTLR JAR的依赖。

使用像Maven这样的东西的一个主要好处是你不再需要链接到磁盘上的物理JAR。相反,你告诉Maven你想要使用ANTLR,声明如下:

<dependency>
    <groupId>org.antlr</groupId>
    <artifactId>antlr4</artifactId>
    <version>4.2</version>
    <!-- If you only need the JAR in test, add the following line too -->
    <scope>test</scope>
</dependency>

使用http://mvnrepository.com/artifact/org.antlr/antlr4/4.2等工具搜索您需要的其他JAR。

听起来你需要了解Maven。它是一款非常酷的产品,但如果没有一些基本的技术诀窍,你就无法真正开始一个项目。