在运行时使用 ClassDefNotFoundError 在 maven 结果中构建 scala-java-antlr 项目 [英] building a scala-java-antlr project in maven results with ClassDefNotFoundError on runtime

查看:22
本文介绍了在运行时使用 ClassDefNotFoundError 在 maven 结果中构建 scala-java-antlr 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 mustache.scala 移植到 scala 2.10,并使用 maven 构建它,使用 antlr3-maven-plugin 而不是 sbt.到目前为止,我得到了 this,并且代码编译成功.但是在运行时,当我尝试执行诸如 new Mustach("hello {{world}}!") 之类的简单操作时,我得到一个 java.lang.NoClassDefFoundError.

I'm trying to port mustache.scala to scala 2.10, and make it built with maven with antlr3-maven-plugin instead of sbt. so far, i got this, and the code is compiled successfully. but at runtime, when i try to execute something simple like new Mustach("hello {{world}}!"), i get a java.lang.NoClassDefFoundError.

奇怪的是,起初,我得到了 Node 类的 NoClassDefFoundError.我不明白为什么 Java 类不在编译的 jar 中,但我很想测试它是否有效.所以我只用 javac 编译了两个 java 类,并调用了 scala REPL,如下所示:

the weird thing, is that at first, i got the NoClassDefFoundError for Node class. i didn't understand why the java classes were'nt in the compiled jar, but i was eager to test if it worked. so i compiled just the two java classes with javac, and invoked the scala REPL as follows:

$scala -cp path/to/compiled/java/classes:path/to/jar-with-dependencies.jar
Welcome to Scala version 2.10.2 (OpenJDK 64-Bit Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import org.monkey.mustache._
import org.monkey.mustache._

scala> new Mustache("hello {{world}}!")
java.lang.NoClassDefFoundError: org/monkey/mustache/MustacheLexer
        at org.monkey.mustache.Mustache.<init>(Mustache.scala:21)
        at org.monkey.mustache.Mustache.<init>(Mustache.scala:32)
        at .<init>(<console>:11)
        at .<clinit>(<console>)
        at .<init>(<console>:7)
        at .<clinit>(<console>)
        at $print(<console>)
        ...

所以现在我在 ANTLR 输出类上遇到 NoClassDefFoundError,所以我看到不仅 java 文件编译输出没有打包在 jar 中,还有 ANTLR 生成的文件.scala 代码依赖于 java 代码以及 ANTLR 生成的代码,所以如果编译成功,那一定意味着在某个时候 java &ANTLR 代码编译成功.

so now i'm getting a NoClassDefFoundError on ANTLR output classes, so I see that not only the java files compile output was not packaged in the jar, but also ANTLR generated files. the scala code is depended on the java code as well as on the ANTLR generated code, so if the compilation succeeds, it must mean that at some point the java & ANTLR code was compiled succefully.

附上相关的maven输出:

the relevant maven output is attached:

$ mvn clean install
  .
  . irrelevant output omitted...
  .
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building mustache_2.10 1.0.5-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.3:clean (default-clean) @ mustache_2.10 ---
[INFO] Deleting file set: /path/to/mustache.scala/target (included: [**], excluded: [])
[INFO] 
[INFO] --- antlr3-maven-plugin:1.0:antlr (default) @ mustache_2.10 ---
[INFO] Processing grammar /path/to/mustache.scala/src/main/antlr/org/monkey/mustache/Mustache.g
[INFO] 
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ mustache_2.10 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /path/to/mustache.scala/src/main/resources
[INFO] 
[INFO] --- scala-maven-plugin:3.1.3:compile (project-resources-execution) @ mustache_2.10 ---
[INFO] /path/to/mustache.scala/src/main/java:-1: info: compiling
[INFO] /path/to/mustache.scala/target/generated-sources/antlr:-1: info: compiling
[INFO] /path/to/mustache.scala/src/main/scala:-1: info: compiling
[INFO] Compiling 8 source files to /path/to/mustache.scala/target/classes at 1371141939993
[INFO] prepare-compile in 0 s
[INFO] compile in 8 s
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mustache_2.10 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default) @ mustache_2.10 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- scala-maven-plugin:3.1.3:compile (compile) @ mustache_2.10 ---
[INFO] /path/to/mustache.scala/src/main/java:-1: info: compiling
[INFO] /path/to/mustache.scala/target/generated-sources/antlr:-1: info: compiling
[INFO] /path/to/mustache.scala/src/main/scala:-1: info: compiling
[INFO] Compiling 8 source files to /path/to/mustache.scala/target/classes at 1371141948773
[INFO] prepare-compile in 0 s
[INFO] compile in 8 s
[INFO] 
  .
  . irrelevant output omitted...
  .
[INFO] 
[INFO] --- maven-jar-plugin:2.2:jar (default-jar) @ mustache_2.10 ---
[INFO] Building jar: /path/to/mustache.scala/target/mustache_2.10-1.0.5-SNAPSHOT.jar
[INFO] 
[INFO] >>> maven-source-plugin:2.2.1:jar (attach-sources) @ mustache_2.10 >>>
[INFO] 
[INFO] --- antlr3-maven-plugin:1.0:antlr (default) @ mustache_2.10 ---
[INFO] No grammars processed; generated files are up to date
[INFO] 
[INFO] <<< maven-source-plugin:2.2.1:jar (attach-sources) @ mustache_2.10 <<<
[INFO] 
[INFO] --- maven-source-plugin:2.2.1:jar (attach-sources) @ mustache_2.10 ---
[INFO] Building jar: /path/to/mustache.scala/target/mustache_2.10-1.0.5-SNAPSHOT-sources.jar
[INFO] 
[INFO] --- maven-install-plugin:2.3:install (default-install) @ mustache_2.10 ---
[INFO] Installing /path/to/mustache.scala/target/mustache_2.10-1.0.5-SNAPSHOT.jar to /path/to/mustache_2.10/1.0.5-SNAPSHOT/mustache_2.10-1.0.5-SNAPSHOT.jar
[INFO] Installing /path/to/mustache.scala/pom.xml to /path/to/.m2/repository/org/monkey/mustache_2.10/1.0.5-SNAPSHOT/mustache_2.10-1.0.5-SNAPSHOT.pom
[INFO] Installing /path/to/mustache.scala/target/mustache_2.10-1.0.5-SNAPSHOT-sources.jar to /path/to/.m2/repository/org/monkey/mustache_2.10/1.0.5-SNAPSHOT/mustache_2.10-1.0.5-SNAPSHOT-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 29.516s
[INFO] Finished at: Thu Jun 13 19:46:06 IDT 2013
[INFO] Final Memory: 16M/214M
[INFO] ------------------------------------------------------------------------

最后一行,我如何从要打包在 jar 中的 java 文件中获取编译后的输出以及编译后的 scala 代码?

buttom line, how do i get the compiled output from java files to be packaged in the jar along with the compiled scala code?

推荐答案

scala 编译器读取源 *.java(在您的日志中明确显示),但它不会编译它们.要编译您的 *.scala,它可以使用 *.java 或 *.class.

The scala compiler read the source *.java (explicit in your log), but it doesn't compile them. To compile your *.scala it can used *.java OR *.class.

生成到 target/generated-sources/antlr 中的类不存在于 target/classes 中.编译不是由 maven-compiler-plugin 完成的.错误使用包括:它是一个过滤器,而不是添加源目录的方法.尝试删除它:你会遇到像

The classes generated into target/generated-sources/antlr don't exist into target/classes. The compilation is not done by maven-compiler-plugin. Wrong use of includes : it's a filter not a way to add sources dir. Try to remove it : you will have compilation error like

<代码>.../MustacheLexer.java:[168,17] 找不到符号[ERROR] 符号:变量类型

抱歉,我不知道/不知道如何修复它.

Sorry I don't know/search how to fix it.

这篇关于在运行时使用 ClassDefNotFoundError 在 maven 结果中构建 scala-java-antlr 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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