2015-05-05 48 views
13

當我運行這段代碼方法參考陣列的clone()導致NoClassDefFoundError的:數組

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)); 

它能正常工作,我得到的輸出

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

但是,如果我更換拉姆達通過爲clone()

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

方法參考我得到一個運行時異常:

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) 

我完全不理解這個。我會認爲方法參考和lambda是等價的。這似乎也是一個非常奇怪的例外。誰能解釋一下?

我正在使用JDK 1.8.0_25。

回答

11

這是bug JDK-8056051。它在8u40被列爲固定。

根據錯誤評論,javac在編譯期間的某個點使用虛擬Array類,並忘記將其重寫爲實際的數組類,從而導致此錯誤。