从命令行运行时Scala NoClassDefFoundError

时间:2014-06-04 15:11:02

标签: scala command-line

我在这里做错了什么?所有的迹象都表明这是一个愚蠢的配置问题,但我无法弄清楚为什么我会发现这个课程无法找到。

> cat Exercise03.scala | head -4
package com.me.scala.chapter04.Exercise03

object Exercise03 extends App {
  override def main(args: Array[String]) {

> scalac Exercise03.scala

> ls -1 com/me/scala/chapter04/*.class
com/me/scala/chapter04/Exercise03$.class
com/me/scala/chapter04/Exercise03.class

> javap -classpath com/me/scala/chapter04 Exercise03 | head -2
Compiled from "Exercise03.scala"
public final class com.me.scala.chapter04.Exercise03 extends java.lang.Object {

> scala -cp com/me/scala/chapter04 com.me.scala.chapter04.Exercise03
No such file or class on classpath: com.me.scala.chapter04.Exercise03   

> scala -cp com/me/scala/chapter04 Exercise03
No such file or class on classpath: com/me/scala/chapter04/Exercise03.class

> scala -cp com/me/scala/chapter04/ Exercise03                                                                                                                                                 
Exception in thread "main" java.lang.NoClassDefFoundError: Exercise03 (wrong name: com/me/scala/chapter04/Exercise03)

1 个答案:

答案 0 :(得分:0)

您没有使用输出路径(-d选项)进行编译,因此您不使用类路径运行它。我不知道为什么javap在你调用时工作,但它显然有一个Scala启动器没有的特殊情况。类路径中的条目是基本目录(或JAR文件),在其中构建路径名,其中每个嵌套包名称是相应嵌套的目录名。

所以,简而言之:

% scalac Foo.scala
% scala Foo

或:

% scalac -d target/main Foo.scala
% scala -classpath target/main Foo