如果设置了CLASSPATH,为什么Scala脚本会失败?

时间:2013-07-01 19:34:09

标签: scala

我有以下Scala 2.10脚本可以正常工作:

#!/bin/bash

classpath="${CLASSPATH}"
unset CLASSPATH

exec ${SCALA_HOME}/bin/scala -cp "${classpath}" "$0" "$@" 2>&1
!#

import stuff

但是当CLASSPATH不是unset时,它会失败,例如:

$ ./setter-for-catan.scala
./setter-for-catan.scala:12: error: not found: object play
import play.api.libs.json.JsArray
       ^

one error found

为什么会这样?

2 个答案:

答案 0 :(得分:2)

scala脚本有一个适度的-debug选项。

使用-Ylog-classpath查看编译器正在使用的内容。

使用-nc说“没有编译服务器守护程序”。

使用fsc -shutdown重新开始。

包更改是令人厌恶的,因此包名称或旧包对象等路径中的意外目录会导致无法解释的构建问题。

使用PathResolver转储它看到的类路径。

包含名称的空目录可能会影响包发现。

${SCALA_HOME}/bin/scala -cp "${classpath}" scala.tools.util.PathResolver
${SCALA_HOME}/bin/scala -cp "${classpath}" scala.tools.util.PathResolver some-args

你会看到类似的东西:

apm@mara:~/tmp/scripts$ ./foo.sh
object Environment {
  scalaHome          = /media/Software/scala-2.10.1 (useJavaClassPath = true)
  javaBootClassPath  = <1122 chars>
  javaExtDirs        = 
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/ext
/usr/java/packages/lib/ext
  javaUserClassPath  = ""
  scalaExtDirs       = 
}
object Defaults {
  scalaHome            = /media/Software/scala-2.10.1
  javaBootClassPath    = 
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/resources.jar
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/rt.jar
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/sunrsasign.jar
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/jsse.jar
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/jce.jar
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/charsets.jar
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/netx.jar
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/plugin.jar
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/rhino.jar
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/jfr.jar
/usr/lib/jvm/java-7-openjdk-amd64/jre/classes
/media/Software/scala-2.10.1/lib/akka-actors.jar
/media/Software/scala-2.10.1/lib/jline.jar
/media/Software/scala-2.10.1/lib/scala-actors.jar
/media/Software/scala-2.10.1/lib/scala-actors-migration.jar
/media/Software/scala-2.10.1/lib/scala-compiler.jar
/media/Software/scala-2.10.1/lib/scala-library.jar
/media/Software/scala-2.10.1/lib/scala-partest.jar
/media/Software/scala-2.10.1/lib/scalap.jar
/media/Software/scala-2.10.1/lib/scala-reflect.jar
/media/Software/scala-2.10.1/lib/scala-swing.jar
/media/Software/scala-2.10.1/lib/typesafe-config.jar
  scalaLibDirFound     = Some(/media/Software/scala-2.10.1/lib)
  scalaLibFound        = /media/Software/scala-2.10.1/lib/scala-library.jar
  scalaBootClassPath   = 
  scalaPluginPath      = /media/Software/scala-2.10.1/misc/scala-devel/plugins
}
 COMMAND: 'scala some-args'
RESIDUAL: 'scala some-args'

答案 1 :(得分:2)

编译器守护程序可能会有一些时髦的状态。尝试fsc -shutdownscala -nc重置守护程序。