N
N
Neonoviiwolf2017-12-18 02:34:59
Java
Neonoviiwolf, 2017-12-18 02:34:59

I'm trying to figure out RxJava on javaFx, "hello world" falls?

Crashes on startup
connected rxjava 2.1.7 and rxjavafx 2.0.2

private Observable<String> observable;
    private Observer<String> server;

    @Override
    public void start(Stage primaryStage) throws Exception {
        pane = FXMLLoader.load(getClass().getResource("sample.fxml"));

        Scene scene = new Scene(pane, 1000, 1000, Color.LIGHTGRAY);

        primaryStage.setScene(scene);
        primaryStage.show();


        server = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable disposable) {

            }

            @Override
            public void onNext(String s) {
                System.out.print(s);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onComplete() {

            }
        };
        observable = Observable.just("hello world");  //тут падает при старте
    }
    @FXML
    Button  add;
 @Override
    public void initialize(URL location, ResourceBundle resources) {

        add.setOnAction(event -> {
            observable.subscribe(server);
        });
}

here it pours
Exception in Application start method
java.lang.reflect.InvocationTargetException
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
  at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
  at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
  at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
  at java.lang.Thread.run(Thread.java:748)
Caused by: 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:335)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  at Main.start(Main.java:59)
  at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
  at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
  at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
  at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
  ... 1 more
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:335)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  ... 22 more
Exception running application Main

plz, I don't understand what's wrong. The lesson looked at how to do it on android, but it seems that there should not be a difference in this example

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Neonoviiwolf, 2017-12-18
@Neonoviiwolf

In short, rxjava just has a Reactive Streams dependency, added it and it worked

D
Dmitry Alexandrov, 2017-12-18
@jamakasi666

one)

Caused by: java.lang.NoClassDefFoundError: org/reactivestreams/Publisher

2) javaFX has its own observable collections that you need to use, they already have the event signature paradigm and the whole ui is tied to them.
3) If you want to do something from the code with the interface from another thread (rx depends on the implementation), then you need to perform such operations in the Platform.runLater (java.lang.Runnable runnable) block in which you are already conjuring.

V
Vladimir Yakushev, 2017-12-18
@VYakushev

Neonoviiwolf , it looks like your library is connected only in the IDE, but not in the JAR. Take any JAR decompiler and check for the presence of the RxJava library. If it is not there, then you need to check the build settings.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question