"main"线程中的异常:java.lang.NoClassDefFoundError: org/reactivestreams/Publisher

3
在创建一个“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 {
    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();
        }
    }
}

build.gradle的依赖关系如下:
dependencies {
    compile "io.reactivex.rxjava2:rxjava:2.1.0"
    // https://mvnrepository.com/artifact/org.reactivestreams/reactive-streams
    compile group: 'org.reactivestreams', name: 'reactive-streams', version: '1.0.0.final'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

完整的堆栈跟踪如下(我认为第一行与此无关,但将其放置以确保):
objc[3423]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java (0x10b6dc4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10d0194e0). One of the two will be used. Which one is undefined.
Exception in thread "main" java.lang.NoClassDefFoundError: org/reactivestreams/Publisher
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at MainApp.main(MainApp.java:11)
Caused by: java.lang.ClassNotFoundException: org.reactivestreams.Publisher
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 13 more

请检查此链接:https://dev59.com/IlgQ5IYBdhLWcg3wSiB1 - Abhishek Verma
我猜这只是一个警告,就像你提供的链接中所提到的一样(或者不是吗?)。并不是根本原因。 - Veneet Reddy
我猜你缺少一个JAR包,可以从这里下载 - https://mvnrepository.com/artifact/org.reactivestreams/reactive-streams/1.0.0.final,并将其包含在你的构建和运行时中。如果有帮助,请告诉我。 - hagrawal7777
@hagrawal 尝试下载并包含jar文件,但仍出现相同的异常。 - Veneet Reddy
很奇怪。不确定你的Gradle使用是否有问题。 - hagrawal7777
显示剩余2条评论
1个回答

3

来自评论:

你不需要在项目中加入Reactive-Streams的依赖,因为RxJava已经在编译时依赖了它。否则,正确的版本是:

compile 'org.reactivestreams:reactive-streams:1.0.0'

同时针对测试兼容性套件:

testCompile 'org.reactivestreams:reactive-streams-tck:1.0.0'
.final是一个发布错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接