Appendix A. Getting started with Strange

A.1 Requirements

Strange is a modular Java library that uses the module concepts introduced in Java 11. To run applications using Strange, you need the Java 11 runtime or higher. Developing applications requires the Java 11 or higher SDK to be installed, which also includes the Java 11 runtime. You can download the Java SDK from http://jdk.java.net. We recommend selecting the latest “ready for use” version (e.g., JDK 17).

Note The Java release cycle is very predictable. Every six months, a new major version is released. In September 2021, JDK 17 was released. In March 2022, JDK 18 will be released. Depending on the current date, it is easy to detect what the latest Java release version is.

If you want to know what version of Java you are using, you can pass the -version parameter to the java command. You will see something similar to the following:

java -version
openjdk version "15" 2020-09-15
OpenJDK Runtime Environment (build 15+36-1562)
OpenJDK 64-Bit Server VM (build 15+36-1562, mixed mode, sharing)

This shows that we are currently using Java 15.

Tip Be sure to download the version that matches your platform (e.g. Linux, macOS, or Windows).

Most Java developers use an integrated development environment (IDE) to create Java applications. The most common IDEs for Java development are Eclipse, Apache NetBeans, and IntelliJ IDEA. Strange is a modular Java library that follows the same rules and conventions as any other Java library, so it can be used out of the box on those IDEs, since they provide support for the Java modular system.

Note If there are different versions of your favorite IDE available, be sure to select a version that uses Java 11 at minimum.

Instead of an IDE, some developers prefer to use command-line tools to create, maintain, and execute applications. Those applications typically use a build tool like Maven or Gradle, and dependencies are declared in specific files: e.g., a pom.xml file for Maven or a build.gradle file for Gradle.

All IDEs provide support for Maven and Gradle. We assume that you are familiar with how your IDE supports Maven or Gradle, and we use command-line-driven Maven or Gradle projects for our examples. You have the choice to run the examples either using the command-line approach or in your favorite IDE, using the IDE-specific Maven and Gradle integration.

A.2 Obtaining and installing the demo code

The examples and demos in this book are available in a Git repository located at https://github.com/johanvos/quantumjava. You can get a local copy of the examples by cloning the repository via command-line git commands, such as

git clone https://github.com/johanvos/quantumjava.git

or via the Git support offered by your favorite IDE.

Cloning the repository creates a directory called quantumjava on your local filesystem. You will notice that this directory contains subdirectories that correspond to the chapters of this book. Please note that the code in those samples is subject to changes and improvements. Also, when there are changes in the Strange library, this might require changes in the demo code as well. However, the code at the time of writing this will still be available if you want to check that out. The README instructions in the GitHub repository explain what to do if this is the case.

A.3 The HelloStrange program

The ch02 directory contains the examples used in chapter 2. The first example we run is in hellostrange. Like all the other examples, this one can be opened in your favorite IDE. As discussed earlier, we use the Maven and Gradle command-line approach in this book. However, if you prefer to run the examples from your IDE, that should work equally well.

Running the program

All the examples in this book can be executed using Maven or using Gradle. A build tool like Maven or Gradle makes it easier to compile and run applications and also takes care of any dependencies your code has. This happens in a transitive approach: if your code depends on other code that depends on still other code, all the required code will be downloaded and processed by the build tools.

Using Maven

Maven is a very stable build tool. With the latest version of Maven, you can compile and run all the examples in this book, and we expect that future versions of Java will keep working with the current versions of Maven. Therefore, it is recommended that you install Maven. The instructions at https://maven.apache.org are self-explanatory.

Once Maven is installed, you can use the mvn command on the command line. If you are using an IDE, it is very likely to have built-in integration for Maven. In that case, you can simply open every example as a project in your IDE and follow the typical flow.

You can run the examples from the command line by going to the directory that contains the example you want to run and entering

mvn clean javafx:run

For example, if you do this in the directory ch02/hellostrange, you will see the following output:

[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------------------------------------------------
[INFO] Building hellostrange 1.0-SNAPSHOT
[INFO] --------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ helloquantum ---
[INFO] Deleting /home/johan/quantumcomputing/manning/public/quantumjava/ch02
 /hellostrange/target
[INFO]
[INFO] >>> javafx-maven-plugin:0.0.7:run (default-cli) > process-classes @
 helloquantum >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @
  helloquantum ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile)@helloquantum
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file ...
[INFO]
[INFO] <<< javafx-maven-plugin:0.0.4:run (default-cli) < process-classes @
 helloquantum <<<
[INFO]
[INFO] --- javafx-maven-plugin:0.0.4:run (default-cli) @ helloquantum ---
Using high-level Strange API to generate random bits
----------------------------------------------------
Generate one random bit, which can be 0 or 1. Result = 1
Generated 10000 random bits, 5085 of them were 0, and 4915 were 1.
[INFO] --------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------------------
[INFO] Total time: 2.389 s
[INFO] Finished at: 2020-10-11T17:02:58+02:00
[INFO] Final Memory: 14M/54M
[INFO] --------------------------------------------------------------------

As you can see, the output from our application is surrounded by informational messages from Maven. If you don’t want to see them, you can add the -q option to the mvn command, which tells Maven to be quiet about its own operations. For example, the command

mvn -q clean javafx:run

returns the following output:

Using high-level Strange API to generate random bits
----------------------------------------------------
Generate one random bit, which can be 0 or 1. Result = 1
Generated 10000 random bits, 4983 of them were 0, and 5017 were 1.

Using Gradle

All the examples contain wrapper scripts that first check whether the correct Gradle version is already installed on the system. If this is not the case, the wrapper script will automatically download and install the required version of Gradle.

If you are using Linux or macOS, the Gradle wrapper script is invoked using

./gradlew

If you are using Windows, the Gradle wrapper script should be invoked via

gradlew.bat

Running the hellostrange demo application is very simple and straightforward. The only thing you have to do is invoke the gradle run task. To avoid duplicating Gradle binaries, there is a single Gradle build file in the root directory of the examples (which contains all the chapters as subdirectories). You can run any example by specifying the chapter and the example name to the Gradle command. On Linux and macOS, this is done by calling

./gradlew ch2:hellostrange:run

And on Windows, this is achieved using

gradlew.bat ch2:hellostrange:run

The result of this action depends on whether you already have the required Gradle version. If you do, the output will look similar to this:

To honour the JVM settings for this build a new JVM will be forked. 
Please consider using the daemon: https://docs.gradle.org/6.5/userguide/gradle_daemon.html.
Daemon will be stopped at the end of the build stopping after processing
 
> Task :run
Using high-level Strange API to generate random bits
----------------------------------------------------
Generate one random bit, which can be 0 or 1. Result = 1
Generated 10000 random bits, 4960 of them were 0, and 5040 were 1.
 
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.5/userguide/command_line_interface.html#sec:command_line_warnings

Similar to with the Maven case, you can suppress the output about Gradle by specifying the -q parameter

./gradlew -q run

in which case the output will look as follows:

Using high-level Strange API to generate random bits
----------------------------------------------------
Generate one random bit, which can be 0 or 1. Result = 1
Generated 10000 random bits, 5039 of them were 0, and 4961 were 1.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.119.66