我从一个班级开始
object A {
import my.Result._
...
}
我会像这样编译并执行它:scalac A.scala; scala -classpath . A
现在我添加了第二个B类,最后是:
package my
object A {
import my.Result._
....
}
和
package my
object B extends Enumeration {
...
}
我用scalac A.scala B.scala
编译它们
但我无法设法执行它。
$ scala -classpath . A
Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: A
at scala.sys.package$.error(package.scala:27)
at scala.tools.nsc.GenericRunnerCommand.scala$tools$nsc$GenericRunnerCommand$$guessHowToRun(GenericRunnerCommand.scala:38)
at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
...
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
这可能是因为类文件现在位于新目录' my
',所以我试图更改类路径。
$ scala -classpath /home/absolute/path/my A
Exception in thread "main" java.lang.NoClassDefFoundError: A (wrong name: my/A)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:803)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
...
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
即使换成新目录也不会有帮助
$ cd my; scala -classpath . A
Exception in thread "main" java.lang.NoClassDefFoundError: A (wrong name: my/A)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:803)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
...
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
进度:
我让它发挥作用,但并不像我想的那样:
$ scalac A.scala B.scala
$ scala
> import my._
> my.A.main(new Array[String](2))
答案 0 :(得分:0)
尝试编译成jar并将jar用作类路径。
编译:
scalac -cp my.jar A.scala B.scala
运行:
scala -cp my.jar A