Why I am getting NoClassDefFoundError: org/reactivestreams/Publisher

sakhunzai picture sakhunzai · Apr 5, 2017 · Viewed 8.2k times · Source

Stream.java

import io.reactivex.*;


public class Stream {

    public static void main(String args[])
    {

      Observable.just("Howdy!").subscribe(System.out::println);

    }
}

build.gradle:

group 'com.sakhunzai'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}

dependencies {
    compile 'io.reactivex.rxjava2:rxjava:2.0.5'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

Exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/reactivestreams/Publisher
....
Caused by: java.lang.ClassNotFoundException: org.reactivestreams.Publisher

I am following the tutorial at page six, except I decided to use gradle instead of maven

Edit

Probably some issue with gradle and Intellij IDEA

Following fixed the issue: settings.gradle:

rootProject.name = 'JavaRx'

include "buildSrc"

Answer

nikmaster picture nikmaster · Apr 5, 2017

The exception means that the class org.reactivestreams.Publisher is not available at runtime. So it is missing in the classpath. You can add to the classpath, by adding the dependency-reference in gradle.

Depending on your used version it should look like:

dependencies {
    compile group: 'org.reactivestreams', name: 'reactive-streams', version: '1.0.0'
    ...<the other depandancies>...
}