Quick Start
Last updated
Last updated
To create your first IC canister, follow instructions from the Dfinity Quick Start location.
You can either use local installation of the Canister SDK or use Motoko Playground.
If you create local project, it will implicitly create the first Motoko file main.mo. The code is a very simple HelloWorld application with one method named greet.
To run this canister code in Motoko Playground just copy and paste this source to the Playground editor and click the Deploy button.
We can start building the first Java application, that will invoke greet method. The source of IC4JHelloWorld project can be found here.
For your Java project you can use either Gradle or Maven build. To include IC4J support in your project include Gradle or Maven dependencies.
Now we can start writing Java code. The easiest way to communicate with the Internet Computer Canister is to use ProxyBuilder.
The ProxyBuilder module creates the Java proxy object Canister based on Java interface with Canister annotations. You can find full source of this interface here.
Next, define the Type of Method for the Canister (UPDATE or QUERY), the Name of the method and define Waiter properties for UPDATE method.
For method arguments we can also define Candid type.
First we have to create the ReplicaTransport object using the URL to your Canister (either local or remote). The we use AgentBuilder to create the Agent Object.
To create the Canister Proxy object use ProxyBuilder create the method with the agent and the Canister Principal arguments and then getProxy method passing Java proxy class.
The full source can be found here.
Next, to call the Internet Computer Canister you will need 2 properties, location URL and Canister ID.
This is an example to read those properties from the application.properties file.
Replace those properties with ones from your deployed canister, either local or remote.
The UPDATE call will run asynchronously and return the Java CompletableFuture object.
Next, the Java project can be built and run using Gradle Script build.gradle. (This build requires Java 1.8; if you are using a different version, make the necessary modifications in the script).
This build script creates Fat Jar with all the required dependencies.
Next run with Java.
The output should look like this.