我面对进一步描述的情况。我有一个传统catalog structure的SBT项目,其中我想使用此library。当我尝试直接在程序中解析字符串时出现问题。
import io.github.facaiy.math.expression.MathExp
object SampleApp extends App {
val str = "1 + 5 / 2"
val ex = MathExp.parse(str)
val output = ex.eval(Map[String, Double]())
println(output)
}
我收到错误消息
sbt:StackOvTest> run
[info] Packaging /home/kost/Prog/MyDev/Scala/StackOvTest/target/scala-2.12/stackovtest_2.12-0.1.0.jar ...
[info] Done packaging.
[info] Running SampleApp
[error] (run-main-1) java.lang.NoClassDefFoundError: scala/util/parsing/combinator/JavaTokenParsers
[error] java.lang.NoClassDefFoundError: scala/util/parsing/combinator/JavaTokenParsers
[error] at java.lang.ClassLoader.defineClass1(Native Method)
[error] at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
[error] at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
[error] at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
[error] at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
[error] at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
[error] at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
[error] at java.security.AccessController.doPrivileged(Native Method)
[error] at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
[error] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[error] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[error] at io.github.facaiy.math.expression.compiler.MathExpCompiler$.apply(MathExpCompiler.scala:13)
[error] at io.github.facaiy.math.expression.MathExp$.parse(MathExp.scala:14)
[error] at SampleApp$.delayedEndpoint$SampleApp$1(SampleApp.scala:6)
[error] at SampleApp$delayedInit$body.apply(SampleApp.scala:3)
[error] at scala.Function0.apply$mcV$sp(Function0.scala:34)
[error] at scala.Function0.apply$mcV$sp$(Function0.scala:34)
[error] at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
[error] at scala.App.$anonfun$main$1$adapted(App.scala:76)
[error] at scala.collection.immutable.List.foreach(List.scala:389)
[error] at scala.App.main(App.scala:76)
[error] at scala.App.main$(App.scala:74)
[error] at SampleApp$.main(SampleApp.scala:3)
[error] at SampleApp.main(SampleApp.scala)
[error] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] at java.lang.reflect.Method.invoke(Method.java:498)
[error] at sbt.Run.invokeMain(Run.scala:93)
[error] at sbt.Run.run0(Run.scala:87)
[error] at sbt.Run.execute$1(Run.scala:65)
[error] at sbt.Run.$anonfun$run$4(Run.scala:77)
[error] at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
[error] at sbt.util.InterfaceUtil$$anon$1.get(InterfaceUtil.scala:10)
[error] at sbt.TrapExit$App.run(TrapExit.scala:252)
[error] at java.lang.Thread.run(Thread.java:748)
[error] Caused by: java.lang.ClassNotFoundException: scala.util.parsing.combinator.JavaTokenParsers
[error] at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[error] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[error] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[error] at java.lang.ClassLoader.defineClass1(Native Method)
[error] at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
[error] at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
[error] at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
[error] at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
[error] at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
[error] at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
[error] at java.security.AccessController.doPrivileged(Native Method)
[error] at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
[error] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[error] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[error] at io.github.facaiy.math.expression.compiler.MathExpCompiler$.apply(MathExpCompiler.scala:13)
[error] at io.github.facaiy.math.expression.MathExp$.parse(MathExp.scala:14)
[error] at SampleApp$.delayedEndpoint$SampleApp$1(SampleApp.scala:6)
[error] at SampleApp$delayedInit$body.apply(SampleApp.scala:3)
[error] at scala.Function0.apply$mcV$sp(Function0.scala:34)
[error] at scala.Function0.apply$mcV$sp$(Function0.scala:34)
[error] at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
[error] at scala.App.$anonfun$main$1$adapted(App.scala:76)
[error] at scala.collection.immutable.List.foreach(List.scala:389)
[error] at scala.App.main(App.scala:76)
[error] at scala.App.main$(App.scala:74)
[error] at SampleApp$.main(SampleApp.scala:3)
[error] at SampleApp.main(SampleApp.scala)
[error] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] at java.lang.reflect.Method.invoke(Method.java:498)
[error] at sbt.Run.invokeMain(Run.scala:93)
[error] at sbt.Run.run0(Run.scala:87)
[error] at sbt.Run.execute$1(Run.scala:65)
[error] at sbt.Run.$anonfun$run$4(Run.scala:77)
[error] at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
[error] at sbt.util.InterfaceUtil$$anon$1.get(InterfaceUtil.scala:10)
[error] at sbt.TrapExit$App.run(TrapExit.scala:252)
[error] at java.lang.Thread.run(Thread.java:748)
[error] java.lang.RuntimeException: Nonzero exit code: 1
[error] at sbt.Run$.executeTrapExit(Run.scala:124)
[error] at sbt.Run.run(Run.scala:77)
[error] at sbt.Defaults$.$anonfun$bgRunTask$5(Defaults.scala:1168)
[error] at sbt.Defaults$.$anonfun$bgRunTask$5$adapted(Defaults.scala:1163)
[error] at sbt.internal.BackgroundThreadPool.$anonfun$run$1(DefaultBackgroundJobService.scala:366)
[error] at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
[error] at scala.util.Try$.apply(Try.scala:209)
[error] at sbt.internal.BackgroundThreadPool$BackgroundRunnable.run(DefaultBackgroundJobService.scala:289)
[error] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[error] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[error] at java.lang.Thread.run(Thread.java:748)
[error] (Compile / run) Nonzero exit code: 1
[error] Total time: 1 s, completed May 25, 2018 6:13:01 PM
在通过build.sbt配置库的情况下,如下所示:
name := "StackOvTest"
version := "0.1.0"
scalaVersion := "2.12.3"
libraryDependencies += "io.github.facaiy" % "math-expression-parser" % "0.0.1"
另外,我尝试下载包含此库的相应jar文件,并将该存档放入StackOvTest / lib目录,但我仍然遇到同样的错误。我注意到一些关于scala解析器组合器的消息。即使在添加
之后libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.7"
没有什么能让它发挥作用。我做错了什么?
答案 0 :(得分:2)
您尝试使用的库看起来没有公共maven存储库中提供的兼容Scala 2.12 jar,请参阅here。
这意味着SBT导入了期望使用Scala 2.11的库的版本,因此在查找那里存在且在2.12中不存在的类时会失败。
通常您使用SBT的%%
符号来获取"右键"库的版本(使用构建中使用的Scala版本的库):
"io.github.facaiy" %% "math-expression-parser" % "0.0.1"
实际上,在这种情况下,如果您在Scala 2.12项目中使用它,您将看到SBT无法找到匹配的版本:
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: io.github.facaiy#math-expression-parser_2.12;0.0.1: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
您在这里没有任何简单的解决方案,要么在项目中使用Scala 2.11,要么您必须自己尝试使用Scala 2.12编译该库的源代码。