开发者

Got NoClassDefFound running sample code

开发者 https://www.devze.com 2023-01-05 09:34 出处:网络
I\'ve tried to run this code under scala 2.7.3 and 2.7.7 and everytime i get this error. What is wrong?

I've tried to run this code under scala 2.7.3 and 2.7.7 and everytime i get this error. What is wrong?

import scala.io._

def toInt(in: String): Option[Int] =
  try {
    Some(Integer.parseInt(in.trim))
  } catch {
    case e: NumberFormatException => None
  }

def sum(in: Seq[String]) = {
  val ints = in.flatMap(s => toInt(s))
   开发者_StackOverflow中文版 ints.foldLeft(0)((a, b) => a + b)
  }

println("Enter some numbers and press ctrl-D (Unix/Mac) ctrl-C (Windows)")
val input = Source.fromInputStream(System.in)
val lines = input.getLines.collect
println("Sum "+sum(lines))

Got NoClassDefFound running sample code


I've tried to isolate the problem and it seems that it has to do with the way you input your values.

For example, the following code snippet works as expected, printing 6.

import scala.io._

def toInt(in: String): Option[Int] =
  try {
    Some(Integer.parseInt(in.trim))
  } catch {
    case e: NumberFormatException => None
  }

def sum(in: Seq[String]) = {
  val ints = in.flatMap(s => toInt(s))
  ints.foldLeft(0)((a, b) => a + b)
}

println(sum(List("1","2","3")))

Therefore, the problem must lie somewhere in the lines to follow. I think the main culprit is what Ctrl-C actually does on Windows combined with how the Scala interpreter runs your scripts. The last line of the screenshot you have attached to the question was the starting point. It suggests that somewhere, a batch job is running and the Ctrl-C has sent a signal to kill that batch job. This is actually none other than the scala interpreter itself (scala.bat). Therefore, when you press Ctrl-C, instead of just killing the input stream, you send a signal to kill the whole batch job running your program!

So the error has something to do with this action. I've looked at how scala runs your script. It seems that a folder named scalascriptXXXXXXXXXXXXXXXXXXX (where XX... represent 19 pseudorandom digits) is created in %TEMP% where all the compiled stuff needed is placed. There, a file called Main$$anon$1$$anonfun$1.class is created -- this represents the anonymous function that is passed to the flatMap function. It seems that this file is created in parallel with the running of the program. Therefore, if you kill your process before it has a chance to do this compilation, you will get the ClassNotFoundException.

Now, it's quite strange that you get this error all the time you run your script. I have managed to get it occasionally. Always though, as soon as I press Ctrl-C I get the Terminate batch job (Y/N)? prompt. Sometimes it is accompanied with an exception. Sometimes not. I think it again has to do with the creation of the Main$$anon$1$$anonfun$1.class. If I wait a while, I get no exception. If I press Ctrl-C straight away after start, I get the exception.

After quite a lot of runs I managed to get a complete stack trace(I think... as the Ctrl-C stops the stack trace from being dumped as well), pointing to the main culprit -- a FileNotFoundException.

scala sum.scala
Enter some numbers and press ctrl-D (Unix/Mac) ctrl-C (Windows)
1
2
3
4
java.lang.NoClassDefFoundError: Main$$anon$1$$anonfun$1
        at Main$$anon$1.sum((virtual file):15)
        at Main$$anon$1.<init>((virtual file):25)
        at Main$.main((virtual file):4)
        at Main.main((virtual file))
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at scala.tools.nsc.ObjectRunner$$anonfun$run$1.apply(ObjectRunner.scala:75)
        at scala.tools.nsc.ObjectRunner$.withContextClassLoader(ObjectRunner.scala:49)
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:74)
        at scala.tools.nsc.ScriptRunner$.scala$tools$nsc$ScriptRunner$$runCompiled(ScriptRunner.scala:381)
        at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:414)
        at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:413)
        at scala.tools.nsc.ScriptRunner$.withCompiledScript(ScriptRunner.scala:351)
        at scala.tools.nsc.ScriptRunner$.runScript(ScriptRunner.scala:413)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:168)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Caused by: java.lang.ClassNotFoundException: Main$$anon$1$$anonfun$1
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
        ... 18 more
Caused by: java.io.FileNotFoundException: C:\Users\****\AppData\Local\Temp\scalascript5827128241389779296\Main$$anon$1$$anonfun$1.class (T
he system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at sun.misc.URLClassPath$FileLoader$1.getInputStream(URLClassPath.java:1001)
        at sun.misc.Resource.cachedInputStream(Resource.java:59)
        at sun.misc.Resource.getByteBuffer(Resource.java:154)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:249)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
        ... 23 more
Terminate batch job (Y/N)? y

I would therefore recommend using a better method to input your values. Maybe try Console.in.readLine? and stop when you get a non-numeric line.

Or, if you really want to indicate the end of a stream, try using Ctrl-Z (Ctrl-C is the kill process signal).

scala sum.scala
Enter some numbers and press ctrl-D (Unix/Mac) ctrl-C (Windows)
1
3
44
5
6
^Z
Sum 59

-- Flaviu Cipcigan


The following works on scala 2.8.0.RC6:


import scala.io._

def toInt(in: String): Option[Int] =

try {
  Some(Integer.parseInt(in.trim))
} catch { case e: NumberFormatException => None}

def sum(in: Iterator[String]) = {
     in.toList.flatMap(s => toInt(s)).foldLeft(0)((a, b) => a + b)
}

println("Enter some numbers and press ctrl-D (Unix/Mac) ctrl-C (Windows)")
val input = Source.fromInputStream(System.in)
val lines = input.getLines
println("Sum "+sum(lines))


scala sum.scala
Enter some numbers and press ctrl-D (Unix/Mac) ctrl-C (Windows)
4
3
5
Sum 12

So perhaps it's a good idea to upgrade.


If Arjan manages to compile and execute your code, then it must be:

  • scala and/or scalac not referencing the scala you think
  • an old .class for sum.scala which is picked up by your classpath

Make sure what scalac and scala reference, and try removing any .class previously generated.

0

精彩评论

暂无评论...
验证码 换一张
取 消