Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala long Strings error

I'm working on Scala 2.10.2. When running the following simple code:

object Test {
  var input  = "**** ANY STRING LONGER THAN 65535 CHARACTERS ****"
  def main(args: Array[String]) {
      println(input)
  }
}

The following error is thrown:

java.lang.ClassFormatError: Unknown constant tag 67 in class file Test$
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at Test.main(05.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
.....

Is this a bug or a weird limitation?

Thanks,

like image 290
centr Avatar asked Dec 09 '22 14:12

centr


2 Answers

Since Scala is using Java classes under the covers, the string length limitation is the maximum length of string literal in Java, which can only be 65535 characters. See accepted answer to this question for more details: Size of Initialisation string in java

like image 189
lreeder Avatar answered Dec 21 '22 11:12

lreeder


That's because of plain old Java limitation. In the class file table CONSTANT_Utf8_info represents the information of the String literals, and several kinds of names: types, members, descriptors and attributes. The format of a CONSTANT_Utf8_info table has only two unsinged bytes for the length of the bytes representing the information of a String literal; I belive that's where such a limitation is set. See more in JVM spec

like image 23
Jk1 Avatar answered Dec 21 '22 10:12

Jk1