“线程中的异常”;“主要”;java.lang.NoClassDefFoundError:org/reactivestreams/Publisher?
在创建hello world程序时,我遇到了这个异常。代码如下:“线程中的异常”;“主要”;java.lang.NoClassDefFoundError:org/reactivestreams/Publisher?,java,multithreading,gradle,classnotfoundexception,rx-java2,Java,Multithreading,Gradle,Classnotfoundexception,Rx Java2,在创建hello world程序时,我遇到了这个异常。代码如下: import io.reactivex.Observable; import io.reactivex.Observer; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; /** * Created by veneet on 30/04/17. */ public class MainApp {
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
/**
* Created by veneet on 30/04/17.
*/
public class MainApp {
public static void main(String[] args) {
// This is where the exception occurs.
Observable<String> observable = Observable.create(e -> {
e.onNext("Hello World!");
e.onNext("Hello World!");
e.onNext("Hello World!");
e.onNext("Hello World!");
e.onNext("Hello World!");
e.onNext("Hello World!");
e.onComplete();
});
Observer<String> observer = new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
System.out.println(s);
}
@Override
public void onError(Throwable e) {
System.err.println(e.getMessage());
}
@Override
public void onComplete() {
}
};
observable.subscribeOn(Schedulers.io());
observable.subscribe(observer);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
完整的stacktrace是这样的(我认为第一行是完全不相关的,但要确保):
从评论中:
您不必包含对反应流的依赖,因为RxJava已经对其具有编译时依赖。否则,正确的版本为:
compile 'org.reactivestreams:reactive-streams:1.0.0'
对于测试兼容性套件:
testCompile 'org.reactivestreams:reactive-streams-tck:1.0.0'
.final
我想是发布错误。检查一下,我想这只是一个警告,正如你在链接中提到的(或者是吗?)。不是根本原因我猜您缺少一个JAR,我猜您可以从这里下载它,并将其包含在您的构建和运行时中。让我知道它是否有帮助。@hagrawal尝试下载并包括jar,同样的例外..奇怪。不确定你的gradle使用是否有问题。
compile 'org.reactivestreams:reactive-streams:1.0.0'
testCompile 'org.reactivestreams:reactive-streams-tck:1.0.0'