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

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

问题描述

我正在尝试将 mustache.scala 移植到scala 2.10,并使其构建用antlr3-maven-plugin而不是sbt的Maven.到目前为止,我已经,并且代码已成功编译.但是在运行时,当我尝试执行诸如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] ------------------------------------------------------------------------

buttom行,如何从java文件中获取编译后的输出,并将其与已编译的scala代码一起打包到jar中?

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.

目标/生成的源/antlr中生成的类在目标/类中不存在.编译不是通过maven-compiler-plugin完成的.错误地使用include:这是一个过滤器,而不是添加源目录的方法.尝试将其删除:您将遇到编译错误,例如

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] cannot find symbol [ERROR] symbol : variable type

.../MustacheLexer.java:[168,17] cannot find symbol [ERROR] symbol : variable type

对不起,我不知道/不知道如何解决它.

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

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

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