3

While creating a hello world program I got this exception. Here is the code:

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

The build.gradle dependencies are like this:

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'
}

The complete stacktrace is like this(I think the first line is totally unrelated, but putting it to ensure):

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
Veneet Reddy
  • 2,707
  • 1
  • 24
  • 40
  • check this http://stackoverflow.com/questions/43003012/objc3648-class-javalaunchhelper-is-implemented-in-both – Abhishek Verma Apr 30 '17 at 14:48
  • I guess this is just a warning, as mentioned in the link you gave(or is it?). Not the root cause – Veneet Reddy Apr 30 '17 at 14:50
  • I guess you are missing a JAR and I guess you can download it from here - https://mvnrepository.com/artifact/org.reactivestreams/reactive-streams/1.0.0.final and include it in your build and runtime. Let me know if it helps. – hagrawal7777 Apr 30 '17 at 15:09
  • @hagrawal Tried downloading and including jar, same exception.. – Veneet Reddy Apr 30 '17 at 15:16
  • Strange. Not sure if there could be some problem with your gradle usage. – hagrawal7777 Apr 30 '17 at 15:21
  • RxJava should pull in the proper dependencies and `compile group: 'org.reactivestreams', name: 'reactive-streams', version: '1.0.0.final'` is wrong; the proper version is '1.0.0'. – akarnokd Apr 30 '17 at 16:43
  • @akarnokd thanks removing this dependency altogether worked! – Veneet Reddy Apr 30 '17 at 16:48

1 Answers1

3

From the comments:

you don't have to include a dependency on Reactive-Streams because RxJava has a compile-time dependency on it already. Otherwise, the right version is:

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

and for the Test Compatibility Kit:

testCompile 'org.reactivestreams:reactive-streams-tck:1.0.0'

The .final was a release mistake I guess.

akarnokd
  • 69,132
  • 14
  • 157
  • 192