Recently, we released support for native Android development for SAFE using a new Java API and we’re excited to see applications from the community!
Java, being platform independent, will run on any platform and since the recent release targeted only Android an interesting question arises, “What about desktop?”
The Java API are indeed supported on desktop too. But however, we are looking into certain aspects of packaging (following maven standards) and some IPC requirements before official release.
Nonetheless, by following the instructions below you can get started with the Java API on desktop. Note that these are not yet finalized and are likely to change.
Building the Java libraries:
The java libs for desktop can be built from the safe_app_java repository. The desktop libraries can be built via the following commands:
On Windows, use gradlew.bat instead of gradlew
git clone https://github.com/maidsafe/safe_app_java chmod +x gradlew gradlew safe-app:download-nativelibs
This command will download all the native libraries that are required. You can now build the java libraries using the following command
This command will run the tests and build the java libraries and output 2 JAR files in the
safe-app-0.1.0-mock safe-app-0.1.0-nonMock safe-app-0.1.0 // Please ignore this one
These JAR files will contain the required classes and native libraries for the two variants (mock / non-mock). You can now include these libraries into your project and use the SAFE APIs
For more information about the APIs check out this Android tutorial and the API documentation.
Note: Both the JARs contain the same class files so adding both the libraries will throw an error.
Authentication with the SAFE browser
The application can authenticate to the SAFE network through the SAFE browser. You can create an auth request by following the authentication section of the tutorial. The only difference on desktop would be that the URL format to send the request to the authenticator would be:
On opening this URI the auth request will be sent to the authenticator in the SAFE browser.
After authentication, the browser will send the auth response back to the application through a custom URI registered for the application in the following format:
The application will need to register itself to open URLs of this type and use the auth response to connect to the network. This URI registration can be done across platforms using the system_uri crate.
With some experimentation I was able to create Java bindings for this crate using jnr-ffi as a proof of concept. The code is available in this repository. A lot more is to be done there in terms of error handling across the FFI but it’s a start You can download the built jars (with native libs) from the releases.
Example CLI application
To demonstrate the authentication process with the SAFE browser I have created a simple Java CLI application which uses the SAFE browser to authenticate and connect to the network. The code is available is this GitHub repository.
To run the application on the mock network:
- Download the latest mock browser from the GitHub releases
- Clone the repository
- Create a
libsfolder in the repository’s root and copy the built
safe-app-0.1.0-mock.jarfile from the
safe_app_java/safe-app/build/libsfolder. You will also need the
system_uri-0.0.1.jarfile from this release.
- Now run the following command:
This will generate a
java-0.0.1.jar file in the
build/libs folder. This executable JAR is packaged along with the
system-uri packages. Now open the downloaded mock browser, create an account and login. On executing the CLI application via:
java -jar <path/to/executable.jar>
an auth request will be sent to the SAFE browser and on authentication the response will be sent back to the CLI application.
Now that you are connected to the network you can perform network operations of your choice by referring the rest of the tutorial or the API docs
OS X Limitation:
Unfortunately, we cannot map an executable JAR to a custom URI on OS X. It needs to be packaged as an application with an
Info.plist file. So this CLI is limited to Windows and Linux(for now). More details about this issue on this thread.