Rxjava 简明教程

RxJava - Environment Setup

Local Environment Setup

RxJava 是 Java 的一个库,因此最基本的要求是你的机器上已安装 JDK。

RxJava is a library for Java, so the very first requirement is to have JDK installed in your machine.

System Requirement

JDK

1.5 or above.

Memory

No minimum requirement.

Disk Space

No minimum requirement.

Operating System

No minimum requirement.

Step 1 - Verify Java Installation in Your Machine

首先,打开控制台并根据您正在使用的操作系统执行 java 命令。

First of all, open the console and execute a java command based on the operating system you are working on.

OS

Task

Command

Windows

Open Command Console

c:> java -version

Linux

Open Command Terminal

$ java -version

Mac

Open Terminal

machine:< joseph$ java -version

让我们验证所有操作系统的输出 −

Let’s verify the output for all the operating systems −

OS

Output

Windows

java version "1.8.0_101" Java™ SE Runtime Environment (build 1.8.0_101)

Linux

java version "1.8.0_101" Java™ SE Runtime Environment (build 1.8.0_101)

Mac

java version "1.8.0_101" Java™ SE Runtime Environment (build 1.8.0_101)

如果你的系统上没有安装 Java,则从以下链接下载 Java 软件开发工具包 (SDK) https://www.oracle.com 。本教程假设已安装 Java 1.8.0_101。

If you do not have Java installed on your system, then download the Java Software Development Kit (SDK) from the following link https://www.oracle.com. We are assuming Java 1.8.0_101 as the installed version for this tutorial.

Step 2 - Set JAVA Environment

JAVA_HOME 环境变量设置为您计算机中安装 Java 的基本目录位置。例如。

Set the JAVA_HOME environment variable to point to the base directory location where Java is installed on your machine. For example.

OS

Output

Windows

Set the environment variable JAVA_HOME to C:\Program Files\Java\jdk1.8.0_101

Linux

export JAVA_HOME = /usr/local/java-current

Mac

export JAVA_HOME = /Library/Java/Home

在系统路径中追加 Java 编译器位置。

Append Java compiler location to the System Path.

OS

Output

Windows

Append the string C:\Program Files\Java\jdk1.8.0_101\bin at the end of the system variable, Path.

Linux

export PATH = $PATH:$JAVA_HOME/bin/

Mac

not required

如上所述,使用命令 java -version 验证 Java 安装。

Verify Java installation using the command java -version as explained above.

Step 3 - Download RxJava2 Archive

RxJava @ MVNRepository 下载 RxJava jar 文件的最新版本及其关联项 Reactive Streams @ MVNRepository 。在本教程的编写时,我们下载了 rxjava-2.2.4.jar 和 reactive-streams-1.0.2.jar,并将其复制到了 C:\>RxJava 文件夹中。

Download the latest version of RxJava jar file from RxJava @ MVNRepository and its dependency Reactive Streams @ MVNRepository . At the time of writing this tutorial, we have downloaded rxjava-2.2.4.jar, reactive-streams-1.0.2.jar and copied it into C:\>RxJava folder.

OS

Archive name

Windows

rxjava-2.2.4.jar, reactive-streams-1.0.2.jar

Linux

rxjava-2.2.4.jar, reactive-streams-1.0.2.jar

Mac

rxjava-2.2.4.jar, reactive-streams-1.0.2.jar

Step 4 - Set RxJava Environment

设置 RX_JAVA 环境变量以指向存储 RxJava jar 的你的机器上的基本目录位置。我们假设已将 rxjava-2.2.4.jar 和 reactive-streams-1.0.2.jar 存储在 RxJava 文件夹中。

Set the RX_JAVA environment variable to point to the base directory location where RxJava jar is stored on your machine. Let’s assuming we’ve stored rxjava-2.2.4.jar and reactive-streams-1.0.2.jar in the RxJava folder.

Sr.No

OS & Description

1

Windows Set the environment variable RX_JAVA to C:\RxJava

2

Linux export RX_JAVA = /usr/local/RxJava

3

Mac export RX_JAVA = /Library/RxJava

Step 5 - Set CLASSPATH Variable

设置 CLASSPATH 环境变量以指向 RxJava jar 位置。

Set the CLASSPATH environment variable to point to the RxJava jar location.

Sr.No

OS & Description

1

Windows Set the environment variable CLASSPATH to %CLASSPATH%;%RX_JAVA%\rxjava-2.2.4.jar;%RX_JAVA%\reactive-streams-1.0.2.jar;.;

2

Linux export CLASSPATH = $CLASSPATH:$RX_JAVA/rxjava-2.2.4.jar:reactive-streams-1.0.2.jar:.

3

Mac export CLASSPATH = $CLASSPATH:$RX_JAVA/rxjava-2.2.4.jar:reactive-streams-1.0.2.jar:.

Step 6 - Test RxJava Setup

像下面所示创建 TestRx.java 类 −

Create a class TestRx.java as shown below −

import io.reactivex.Flowable;
public class TestRx {
   public static void main(String[] args) {
      Flowable.just("Hello World!").subscribe(System.out::println);
   }
}

Step 7 - Verify the Result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\RxJava>javac Tester.java

验证输出。

Verify the output.

Hello World!