{"id":13724738,"url":"https://github.com/wasmerio/wasmer-java","last_synced_at":"2025-06-25T14:05:14.138Z","repository":{"id":38331947,"uuid":"205020468","full_name":"wasmerio/wasmer-java","owner":"wasmerio","description":"☕ WebAssembly runtime for Java","archived":false,"fork":false,"pushed_at":"2022-06-08T09:03:30.000Z","size":328,"stargazers_count":626,"open_issues_count":23,"forks_count":56,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-06-22T06:15:24.508Z","etag":null,"topics":["java","java-library","jni","rust","wasm","wasmer","webassembly"],"latest_commit_sha":null,"homepage":"https://medium.com/wasmer/announcing-the-first-java-library-to-run-webassembly-wasmer-jni-89e319d2ac7c","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wasmerio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-28T20:46:27.000Z","updated_at":"2025-06-14T21:59:26.000Z","dependencies_parsed_at":"2022-07-17T00:16:00.161Z","dependency_job_id":null,"html_url":"https://github.com/wasmerio/wasmer-java","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/wasmerio/wasmer-java","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wasmerio%2Fwasmer-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wasmerio%2Fwasmer-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wasmerio%2Fwasmer-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wasmerio%2Fwasmer-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wasmerio","download_url":"https://codeload.github.com/wasmerio/wasmer-java/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wasmerio%2Fwasmer-java/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261888088,"owners_count":23225138,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","java-library","jni","rust","wasm","wasmer","webassembly"],"created_at":"2024-08-03T01:02:02.697Z","updated_at":"2025-06-25T14:05:14.096Z","avatar_url":"https://github.com/wasmerio.png","language":"Java","readme":"\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://wasmer.io\" target=\"_blank\" rel=\"noopener noreferrer\"\u003e\n    \u003cimg width=\"300\" src=\"https://raw.githubusercontent.com/wasmerio/wasmer/master/assets/logo.png\" alt=\"Wasmer logo\"\u003e\n  \u003c/a\u003e\n  \n  \u003ch1\u003eWasmer Java\u003c/h1\u003e\n  \n  \u003cp\u003e\n    \u003ca href=\"https://github.com/wasmerio/wasmer-java/actions?query=workflow%3A%22Build+and+Test%22\"\u003e\n      \u003cimg src=\"https://github.com/wasmerio/wasmer-java/workflows/Build%20and%20Test/badge.svg\" alt=\"Build Status\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/wasmerio/wasmer-java/blob/master/LICENSE\"\u003e\n      \u003cimg src=\"https://img.shields.io/github/license/wasmerio/wasmer-java.svg\" alt=\"License\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://bintray.com/wasmer/wasmer-jni/wasmer-jni\"\u003e\n      \u003cimg src=\"https://img.shields.io/badge/bintray-package-f06\" alt=\"Go Package\"\u003e\n    \u003c/a\u003e \n    \u003ca href=\"https://wasmerio.github.io/wasmer-java/\"\u003e\n      \u003cimg src=\"https://img.shields.io/badge/documentation-API-ff0066.svg\" alt=\"API Documentation\"\u003e\n    \u003c/a\u003e \n  \u003c/p\u003e\n\n  \u003ch3\u003e\n    \u003ca href=\"https://wasmer.io/\"\u003eWebsite\u003c/a\u003e\n    \u003cspan\u003e • \u003c/span\u003e\n    \u003ca href=\"https://docs.wasmer.io\"\u003eDocs\u003c/a\u003e\n    \u003cspan\u003e • \u003c/span\u003e\n    \u003ca href=\"https://slack.wasmer.io/\"\u003eSlack Channel\u003c/a\u003e\n  \u003c/h3\u003e\n\n\u003c/div\u003e\n\n\u003chr/\u003e\n\nA complete and mature WebAssembly runtime for Java based on [Wasmer].\n\nFeatures:\n\n  * **Easy to use**: The `wasmer` API mimics the standard WebAssembly API,\n  * **Fast**: The Wasmer JNI executes the WebAssembly modules as fast as\n    possible, close to **native speed**,\n  * **Safe**: All calls to WebAssembly will be fast, but more\n    importantly, completely safe and sandboxed.\n\n[Wasmer]: https://github.com/wasmerio/wasmer\n\n# Install\n\nThe Wasmer package is published in [Bintray](https://bintray.com/) on [the\n`wasmer/wasmer-jni`\nrepository](https://bintray.com/wasmer/wasmer-jni/wasmer-jni).\n\nThe JAR files are named as follows:\n`wasmer-jni-$(architecture)-$(os)-$(version).jar`. Thus, to include\nWasmer JNI as a dependency, write for instance:\n\n```gradle\ndependencies {\n    implementation \"org.wasmer:wasmer-jni-amd64-linux:0.3.0\"\n}\n```\n\n\u003e Note: It is also possible to download the Java JAR file from the [Github\nreleases page](https://github.com/wasmerio/wasmer-java/releases)!\n\u003e If you need to produce a JAR for your own platform and\narchitecture, see [the Development Section](#development) to learn\nmore.\n\n# Example\n\nThere is a toy program in `java/src/test/resources/simple.rs`, written\nin Rust (or any other language that compiles to WebAssembly):\n\n```rust\n#[no_mangle]\npub extern fn sum(x: i32, y: i32) -\u003e i32 {\n    x + y\n}\n```\n\nAfter compilation to WebAssembly, the\n[`tests/resources/simple.wasm`](https://github.com/wasmerio/wasmer-java/blob/master/tests/resources/simple.wasm)\nbinary file is generated. ([Download\nit](https://github.com/wasmerio/wasmer-java/raw/master/tests/resources/simple.wasm)).\n\nThen, we can execute it in Java:\n\n```java\nclass Example {\n    public static void main(String[] args) {\n        // `simple.wasm` is located at `tests/resources/`.\n        Path wasmPath = Paths.get(new Example().getClass().getClassLoader().getResource(\"simple.wasm\").getPath());\n\n        // Reads the WebAssembly module as bytes.\n        byte[] wasmBytes = Files.readAllBytes(wasmPath);\n\n        // Instantiates the WebAssembly module.\n        Instance instance = new Instance(wasmBytes);\n\n        // Calls an exported function, and returns an object array.\n        Object[] results = instance.exports.getFunction(\"sum\").apply(5, 37);\n\n        System.out.println((Integer) results[0]); // 42\n\n        // Drops an instance object pointer which is stored in Rust.\n        instance.close();\n    }\n}\n```\n\nThere is more examples in the `examples/` directory. Run them with the\n`Makefile`, such as: `make run-example EXAMPLE=Simple` to run the\n`SimpleExample` example.\n\n# API of the `wasmer` library\n\nThe root namespace is `org.wasmer`.\n\n## The `Instance` class\n\nThe `Instance` constructor compiles and instantiates a WebAssembly\nmodule. It is built upon bytes. From here, it is possible to call\nexported functions, or exported memories. For example:\n\n```java\n// Instantiates the WebAssembly module.\nInstance instance = new Instance(wasmBytes);\n\n// Calls an exported function.\nObject[] results = instance.exports.getFunction(\"sum\").apply(1, 2);\n\n// Casts an object to an integer object because the result is an object array.\nint result = (Integer) results[0];\n\nSystem.out.println(result); // 3\n\n// Drops an instance object pointer manually. Note that the garbage collector\n// will call this method before an object is removed from the memory.\ninstance.close();\n```\n\n### Exports\n\nAll exports, like functions or memories, are accessible on the\n`Instance.exports` field, which is of kind `Exports` (a read-only\nwrapper around a map of kind `Map\u003cString, exports.Export\u003e`). The\n`Exports.get` method returns an object of type `Export`. To\ndowncast it to an exported function or to an exported memory, you can\nuse the respective `getFunction` or `getMemory` methods. The following\nsections describe each exports in details.\n\n#### Exported functions\n\nAn exported function is a native Java closure (represented by the\n`exports.Function` class), where all arguments are automatically\ncasted to WebAssembly values if possible, and all results are of type\n`Object`, which can be typed to `Integer` or `Float` for instance.\n\n```java\nFunction sum = instance.exports.getFunction(\"sum\");\nObject[] results = sum.apply(1, 2);\n\nSystem.out.println((Integer) results[0]); // 3\n```\n\n#### Exported memories\n\nAn exported memory is a regular `Memory` class.\n\n```java\nMemory memory = instance.exports.getMemory(\"memory_1\");\n```\n\nSee the [`Memory`](#the-memory-class) class section for more information.\n\n## The `Module` class\n\nThe `Module.validate` static method checks whether a sequence of bytes\nrepresents a valid WebAssembly module:\n\n```java\n// Checks that given bytes represent a valid WebAssembly module.\nboolean isValid = Module.validate(wasmBytes);\n```\n\nThe `Module` constructor compiles a sequence of bytes into a\nWebAssembly module. From here, it is possible to instantiate it:\n\n```java\n// Compiles the bytes into a WebAssembly module.\nModule module = new Module(wasmBytes);\n\n// Instantiates the WebAssembly module.\nInstance instance = module.instantiate();\n```\n\n### Serialization and deserialization\n\nThe `Module.serialize` method and its complementary\n`Module.deserialize` static method help to respectively serialize and\ndeserialize a _compiled_ WebAssembly module, thus saving the compilation\ntime for the next use:\n\n```java\n// Compiles the bytes into a WebAssembly module.\nModule module1 = new Module(wasmBytes);\n\n// Serializes the module.\nbyte[] serializedModule = module1.serialize();\n\n// Let's forget about the module for this example.\nmodule1 = null;\n\n// Deserializes the module.\nModule module2 = Module.deserialize(serializedModule);\n\n// Instantiates and uses it.\nObject[] results = module2.instantiate().exports.getFunction(\"sum\").apply(1, 2);\n\nSystem.out.println((Integer) results[0]); // 3\n```\n\n## The `Memory` class\n\nA WebAssembly instance has a linear memory, represented by the\n`Memory` class. Let's see how to read it. Consider the following Rust\nprogram:\n\n```rust\n#[no_mangle]\npub extern fn return_hello() -\u003e *const u8 {\n    b\"Hello, World!\\0\".as_ptr()\n}\n```\n\nThe `return_hello` function returns a pointer to a string. This string\nis stored in the WebAssembly memory. Let's read it.\n\n```java\nInstance instance = new Instance(wasmBytes);\n\n// Gets the memory by specifying its exported name.\nMemory memory = instance.exports.getMemory(\"memory\");\n\n// Gets the pointer value as an integer.\nint pointer = (Integer) instance.exports.getFunction(\"return_hello\").apply()[0];\n\n// Reads the data from the memory.\nByteBuffer memoryBuffer = memory.buffer();\nbyte[] stringBytes = new byte[13];\nmemoryBuffer.position(pointer);\nmemoryBuffer.get(stringBytes);\n\nSystem.out.println(new String(stringBytes)); // Hello, World!\n\ninstance.close();\n```\n\n### Memory grow\n\nThe `Memory.grow` methods allows to grow the memory by a number of pages (of 64KiB each).\n\n```java\n// Grows the memory by the specified number of pages, and returns the number of old pages.\nint oldPageSize = memory.grow(1);\n```\n\n## Development\n\nThe Wasmer JNI library is based on the [Wasmer\nruntime](https://github.com/wasmerio/wasmer), which is written in\n[Rust](https://www.rust-lang.org/), and is compiled to a shared\nlibrary. For your convenience, we produce one JAR (Java Archive) per\narchitecture and platform. By now, the following are supported,\nconsistently tested, and pre-packaged:\n\n- `amd64-darwin`,\n- `amd64-linux`,\n- `amd64-windows`.\n\nMore architectures and more platforms will be added in a close\nfuture. If you need a specific one, [feel free to\nask](https://github.com/wasmerio/wasmer-java/issues/new?assignees=\u0026labels=%F0%9F%8E%89+enhancement\u0026template=---feature-request.md\u0026title=)!\n\nIf you want to build the extension you will need the following tools:\n\n- [Gradle](https://gradle.org/), a package management tool,\n- [Rust](https://rust-lang.org/), the Rust programming language,\n- [Java](https://www.java.com/), because it's a Java project ;-).\n\n```sh\n$ git clone https://github.com/wasmerio/wasmer-java/\n$ cd wasmer-java\n```\n\nTo build the entire project, run the following command:\n\n```sh\n$ make build\n```\n\nTo build the JAR package:\n\n```sh\n$ make package\n```\n\nThis will generate the file `build/libs/wasmer-jni-$(architecture)-$(os)-0.3.0.jar`.\n\n#### Automatic dependencies per architecture and platform\n\n\u003cdetails\u003e\n\u003csummary\u003eIt is possible to infer the archive appendix automatically, see how.\u003c/summary\u003e\n\nAccording the [Gradle Jar\nAPI](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:appendix),\nthe `$(architecture)-$(os)` part is called the _archive prefix_. To\ninfer that appendix automatically to configure your dependencies, you\ncan use the following `inferWasmerJarAppendix` function:\n\n```gradle\nString inferWasmerJarAppendix() {\n    def nativePlatform = new org.gradle.nativeplatform.platform.internal.DefaultNativePlatform(\"current\")\n    def arch = nativePlatform.architecture\n    def os = nativePlatform.operatingSystem\n\n    def arch_name\n\n    switch (arch.getName()) {\n        case [\"x86_64\", \"x64\", \"x86-64\"]:\n            arch_name = \"amd64\"\n            break;\n\n        default:\n            throw new RuntimeException(\"`wasmer-jni` has no pre-compiled archive for the architecture \" + arch.getName())\n    }\n\n    def os_name\n\n    if (os.isMacOsX()) {\n        os_name = \"darwin\"\n    } else if (os.isLinux()) {\n        os_name = \"linux\"\n    } else if (os.isWindows()) {\n        os_name = \"windows\"\n    } else {\n        throw new RuntimeException(\"`wasmer-jni` has no pre-compiled archive for the platform \" + os.getName())\n    }\n\n    return arch_name + \"-\" + os_name\n}\n```\n\nFinally, you can configure your dependencies such as:\n\n```gradle\ndependencies {\n    implementation \"org.wasmer:wasmer-jni-\" + inferWasmerJarAppendix() + \":0.3.0\"\n}\n```\n\n\u003c/details\u003e\n\n### Testing\n\nRun the following command:\n\n```sh\n$ make test\n```\n\nNote: Testing automatically builds the project.\n\n### Documentation\n\nRun the following command:\n\n```sh\n$ make javadoc\n```\n\nThen open `build/docs/javadoc/index.html`.\n\n# What is WebAssembly?\n\nQuoting [the WebAssembly site](https://webassembly.org/):\n\n\u003e WebAssembly (abbreviated Wasm) is a binary instruction format for a\n\u003e stack-based virtual machine. Wasm is designed as a portable target\n\u003e for compilation of high-level languages like C/C++/Rust, enabling\n\u003e deployment on the web for client and server applications.\n\nAbout speed:\n\n\u003e WebAssembly aims to execute at native speed by taking advantage of\n\u003e [common hardware\n\u003e capabilities](https://webassembly.org/docs/portability/#assumptions-for-efficient-execution)\n\u003e available on a wide range of platforms.\n\nAbout safety:\n\n\u003e WebAssembly describes a memory-safe, sandboxed [execution\n\u003e environment](https://webassembly.org/docs/semantics/#linear-memory) […].\n\n# License\n\nThe entire project is under the MIT License. Please read [the\n`LICENSE` file][license].\n\n[license]: https://github.com/wasmerio/wasmer/blob/master/LICENSE\n","funding_links":[],"categories":["Java"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwasmerio%2Fwasmer-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwasmerio%2Fwasmer-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwasmerio%2Fwasmer-java/lists"}