9

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"
sakhunzai
  • 13,900
  • 23
  • 98
  • 159

3 Answers3

6

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>...
}
nikmaster
  • 421
  • 1
  • 6
  • 19
  • but gradle refuses to import the dependency , does nothing – sakhunzai Apr 05 '17 at 13:05
  • oh, sorry, as far as I see now, you reference to maven central. so the example from https://search.maven.org should work better. (before I used "my" known repository as reference). hope this works. If not, you should check your really used repository (to see, how this depandancy is defined in your repository). So, please try: compile 'org.reactivestreams:reactive-streams:1.0.0.final' – nikmaster Apr 05 '17 at 13:13
  • The question is why I should care about dependencies, isn't that is the task of Gradle (to fetch associated dependencies) ? – sakhunzai Apr 06 '17 at 13:02
  • gradle (and similar maven and other dependency management tools) will provide the dependencies to the classpath. But they need a list of dependencies, which should be resolved by using the libs from the repository. Of course transitive dependencies are possible, if they are declared (So if A need B, and B needs C, it will resolve B and C for A). As a result dependency management still need a list of deps (with version), it can't even know your used libs and version of the libs). sorry, if I have misunderstood your comment – nikmaster Apr 06 '17 at 17:34
3

I got this exception using org.springframework.test.web.reactive.server.WebTestClient so it's easy to fix with spring-boot, just adding this dependency to your pom.xml (for maven projects):

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
Alan Teals
  • 451
  • 4
  • 8
0

Forcing the version of reactive-streams to 1.0.0 in gradle works for me.

compile("org.reactivestreams:reactive-streams:1.0.0"){
    force = true
}
Iceberg
  • 2,744
  • 19
  • 19