why does a scala class in the worksheet with same name but different case as the worksheet cause an exception?

StackOverflow https://stackoverflow.com/questions/20196388

  •  04-08-2022
  •  | 
  •  

سؤال

When i have the following class i get an exception.

object rational {
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
  val x = new Rational(1,2)                       //> java.lang.NoClassDefFoundError: Rational (wrong name: rational)
                                                  //|   at java.lang.ClassLoader.defineClass1(Native Method)
                                                  //|   at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
                                                  //|   at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
                                                  //|   at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
                                                  //| 1)
                                                  //|   at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
                                                  //|   at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
                                                  //|   at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
                                                  //|   at java.security.AccessController.doPrivileged(Native Method)
                                                  //|   at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
                                                  //|   at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
                                                  //|   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
                                                  //|   at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
                                                  //|   at rational$$anonfun$main$1.apply$mcV$sp(rational.scala:3)
                                                  //|   at org.scalaide.worksheet.runtime.library.WorksheetSupport$$ano
                                                  //| Output exceeds cutoff limit.
}

class Rational (x:Int, y:Int) {
    def numer = x
    def denom = y

    def add(that:Rational) =
    new Rational (
            numer * that.denom + denom * that.numer, denom * that.denom
        )

        override def toString() = numer + "/" + denom
}

When i change the name of object rational to rationals everything is fine

هل كانت مفيدة؟

المحلول

This is probably because of the type-insensitivity of the Windows file system. This would get messed up in Java too. The compiled .class files for the upper- and lower-case versions are indistinguishable and cannot both exist in the filesystem at the same time.

The object rational generates both a rational$.class and a rational.class.

The class Rational generates a Rational.class. This appears to be being overwritten by rational.class. When the JVM tries to read Rational.class it finds rational.class, sees that the content is wrong, and complains bitterly about having been led astray.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top