Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method reference to array clone() causes NoClassDefFoundError: Array

When I run this code

List<int[]> list = Arrays.asList(new int[]{1, 2, 3}, new int[]{4, 5});
int[][] arr = list.stream().map(j -> j.clone()).toArray(int[][]::new);
System.out.println(Arrays.deepToString(arr));

it works as expected and I get the output

[[1, 2, 3], [4, 5]]

However, if I replace the lambda by a method reference for clone()

int[][] arr = list.stream().map(int[]::clone).toArray(int[][]::new);

I get a runtime exception:

Exception in thread "main" java.lang.NoClassDefFoundError: Array
    at Main.lambda$MR$main$clone$8ed4b78b$1(Main.java:14)
    at Main$$Lambda$1/1160460865.apply(Unknown Source)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:576)
    at java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:255)
    at java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:438)
    at Main.main(Main.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: Array
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

I don't understand this at all. I would have thought the method reference and the lambda were equivalent. Also it seems a really strange exception to get. Can anyone explain?

I am using JDK 1.8.0_25.

like image 453
Paul Boddington Avatar asked May 05 '15 22:05

Paul Boddington


1 Answers

This is bug JDK-8056051. It's listed as fixed in 8u40.

Based on the bug comments, javac uses a dummy Array class at some point during compilation and forgets to rewrite it to the actual array class, causing this error.

like image 188
Jeffrey Bosboom Avatar answered Oct 20 '22 16:10

Jeffrey Bosboom