Example use of iota.rs Java bindings
- Download or clone the
iota.rs
repository
$ git clone https://github.com/iotaledger/iota.rs.git
In order to build with the Java bindings, you need the following two parts:
- Java classes containing
native
methods which call C code. (.jar
) - JNI bindings linking
Rust
toC
, and thenC
to javanative
methods (.so
,.dll
or.dylib
depending on your system)
mvn install:install-file -Dfile=/path/to/iota.rs/bindings/java/native/build/libs/native.jar -DgroupId=org.iota.client -DartifactId=native -Dversion=1.0 -Dpackaging=jar
<dependency>
<groupId>org.iota.client</groupId>
<artifactId>native</artifactId>
<version>1.0<version>
</dependency>
With a pre-made jar
- Point to the JAR in
build.gradle
dependencies
section usingimplementation files("native.jar")
Building the jar through gradle in iota.rs
creates the jar at iota.rs/bindings/java/native/build/libs
Directly pointing to the iota.rs project
- Uncomment the lines in
settings.gradle
, then: - Change
settings.gradle
to point to the\native
project insideiota.rs\bindings\java
, so we can load the Java files - Add
implementation project(':native')
to thedependencies
section of yourbuild.gradle
(and commentimplementation files("native.jar")
if you have it)
This file can be found at iota.rs/bindings/java/target/release
after building the bindings with cargo build --release
in the iota.rs/bindings/java
folder. We will call this file iota_client
for the purpose of this README.
Adding the folder to your PATH is the easiest way to ensure it is available.
- Change to your home directory.
cd $HOME
. - Open the
.bashrc
file. - Add the following line to the file:
export PATH=/path/to/iota_client:$PATH
. - Save the file and exit. Use the
source
command to force Linux to reload the.bashrc
- Open the Start Search, type in “env”, and choose “Edit the system environment variables”
- Click the “Environment Variables…” button.
- Under the “System Variables” section (the lower half), find the row with “Path” in the first column, and click edit.
- The “Edit environment variable” UI will appear. Click “New” and type in the new path:
/path/to/iota_client
- Dismiss all of the dialogs by choosing “OK”. Your changes are saved!
- Open up Terminal.
- Run the following command: sudo nano /etc/paths.
- Enter your password, when prompted.
- Go to the bottom of the file, and enter the path you wish to add.
- Hit control-x to quit.
- Enter “Y” to save the modified buffer.
We need to make sure the file is added to java.library.path
before building or running.
To do this, we add/update the section below to our pom.xml
The ${basedir}
means we need to place the iota_client
file in the base of our repo. (Where your pom.xml is)
Alternatively, you can replace ${basedir}
with an absolute path to the file: /path/to/iota_client
<build>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>[VERSION]</version>
<configuration>
<argLine>-Djava.library.path=${basedir}</argLine>
</configuration>
</plugin>
</build>
Modify build.gradle
variable iotaLibLocation
to the location of iota_client
.
Run mvn compile
to build.
Run mvn exec:java -Dexec.mainClass="org.example.ExampleApp"
to run. (You can also add the mainclass to your pom using the exec-maven-plugin
plugin)
Run mvn test
to specifically run the test.
Run gradle build
to build.
Run gradle run
to run. (linking directly to the iota.rs for jar triggers a rebuild every time)
Run gradle test
to specifically run the test.
As the API is made to be as close as possible to the rust API, the most up to date documentation can be found here, until a pure Java version is made.
YOu can also generate more up-to-date documentation by compiling it yourself. Instructions can be found [here]](https://github.com/iotaledger/iota.rs/tree/dev/documentation#readme)
The java methods are made to almost 1:1 correspond to rust version. Difference beeing the naming convention (Rust beeing snake_case, java camelCase)
Due to the fact that we are linking through C from Rust, there are a couple of limiting factors.
- Classic builder patterns return a
clone
after each builder call since we can only pass back to C by reference inRust
Builder builder1 = new Builder();
Builder builder2 = builder1.setValue(true);
// These are different instances, thus builder1 wont have the value set
assertNotEquals(builder1, builder2);