我正在使用这个库(https://github.com/scala/scala-java8-compat)将Scala Future转换为Java 8 CompletableFuture。
但是运行这个例子:
package utils;
import akka.dispatch.Futures;
import org.junit.Test;
import scala.concurrent.Future;
import java.util.concurrent.CompletableFuture;
import scala.compat.java8.FutureConverters;
import scala.concurrent.Promise;
public class ScalaFuture {
@Test
public void test() throws InterruptedException {
Promise promise = Futures.promise();
Future scalaFuture = promise.future();
CompletableFuture cf = FutureConverters.toJava(scalaFuture).toCompletableFuture();
cf.whenComplete((result, t) -> {
System.out.println(result);
});
promise.success("works");
Thread.sleep(1000);
}
我有这个错误
java.lang.NoClassDefFoundError: scala/Function1$class
at scala.concurrent.java8.FuturesConvertersImpl$CF.<init>(FutureConvertersImpl.scala:18)
at scala.compat.java8.FutureConverters$.toJava(FutureConverters.scala:60)
at scala.compat.java8.FutureConverters.toJava(FutureConverters.scala)
at utils.ScalaFuture.test(ScalaFuture.java:19)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: scala.Function1$class
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
... 26 more
知道我的代码有什么问题吗?