Skip to content

Getting Started

Volker Berlin edited this page Dec 29, 2019 · 11 revisions

Getting Started with JWebAssembly

Add Dependency to API

First you must add the references of the JWebAssembly API to a new Java project in your IDE.

Latest release: de.inetsoftware:jwebassembly-api:+

Or Snapshot: com.github.i-net-software:jwebassembly-api:master-SNAPSHOT

For Gradle this can look like:

repositories {
    jcenter()
    maven { url 'https://jitpack.io' } // for snapshot of the API
}
dependencies {
    compile 'com.github.i-net-software:jwebassembly-api:master-SNAPSHOT'
}

For Maven this can look like:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependency>
    <groupId>com.github.i-net-software</groupId>
    <artifactId>jwebassembly-api</artifactId>
    <version>master-SNAPSHOT</version>
</dependency>

Tip: Until a stable version is available the snapshot version is the better choice.

Content of the API

The library jwebassembly-api contains:

  • some important annotations. We will see it use later.
  • Replacements for native Java methods.
  • API to access the DOM.

You can also write or use an alternative API. The compiler is independent of the API.

First Class (Hello World)

Create a first class and a first method with your IDE in your project and add the annotation @Export. The annotation mark this method as callable from JavaScript. You can use any method name for this first method. But it name will be used in JavaScript. The class name and package unimportant.

@Export
public static void main() {
    Document document = Window.document();
    HTMLElement div = document.createElement("div");
    Text text = document.createTextNode("Hello World, this text come from WebAssembly."); 
    div.appendChild( text );
    document.body().appendChild( div );
}

If your browser support it then you can see the running Hello World sample. The complete sample source code can you see in the github repository.

Compiler Output

In the hello world sample we see that the compiler has create the files:

File Meaning
HelloWorld.java The java source file. After compiling only needed for debugging in the browser.
HelloWorld.wasm The WebAssembly file.
HelloWorld.wasm.js The JavaScript API that the WebAssembly code is importing.
HelloWorld.wasm.map Optional source map file, only needed for debugging.
HelloWorld.html The main file to load the WebAssembly file. Currently it is not possible to load an assembly file directly.

HTML template

Clone this wiki locally