Git Product home page Git Product logo

zig-jni's Introduction

Zig-JNI

Zig-JNI is a thin wrapper around the Java Native Interface (JNI) for the Zig programming language. It is designed to be a simple and easy-to-use interface for implementing Java native methods in Zig.

Please note that this project is still in the early stages of development, and it is not yet ready for production use. This project was initially created as a part of another project of mine which provides a write-one-run-anywhere solution for GPGPU programming using java language, and I will continue to improve it as I work on that project.

Since this project is licensed under the MIT license, you are free to use it in your own projects, but please be aware that it is still a work in progress, and it may contain bugs or other issues. If you encounter any problems, please feel free to open an issue or submit a pull request.

Why Zig?

Zig is a modern, general-purpose programming language with a focus on safety, performance, and simplicity. It is designed to be a better C, and it is a great fit for implementing native methods in Java.

The important features of Zig, especially for JNI development, are:

  • First-class support for cross-compilation: Zig can compile code for any platform from any platform, and it can do so with a single command. This makes it easy to build JNI libraries for multiple platforms from a single machine.
  • No runtime: Zig has no runtime, and it does not require a runtime to be present on the target system. This makes it easy to build small, self-contained JNI libraries that can be easily distributed and used.
  • C & C++ interoperability: Zig has first-class support for interoperating with C and C++ code, which makes it easy to work with the JNI API and other C libraries.

Platform Support

Linux Windows macOS
x86_64
aarch64
armv7
  • ✅ Supported
  • ❓ Untested, but should work
  • ❌ Not supported

Import the library

First, add Zig-JNI to your build.zig.zon file (Don't forget to replace {VERSION} with the version you want to use) :

.{
    .name = "...",
    .version = "...",
    .dependencies = .{
        .JNI = .{
            .url = "https://github.com/SuperIceCN/Zig-JNI/archive/refs/tags/{VERSION}.tar.gz",
        }
    },
}

Second, Run zig build in your project, and the compiler will instruct you to add a .hash = "..." field next to .url. The instruction will look like this:

note: expected .hash = "1220a61eebdcda95a9e51e2a4f237522d407a0404aea4225fc27b2bb1d32f27edf9c",

Third, use the dependency in your build.zig :

pub fn build(b: *std.Build) void {
    // depdencies
    const dep_JNI = b.dependency("JNI", .{}).module("JNI");
    // ...
    artifact.root_module.addImport("jni", dep_JNI); 
    // here artifact is the return value of b.addExecutable / b.addSharedLibrary / b.addStaticLibrary
    // ...
}

Example

Here is a simple example of how to use Zig-JNI to implement a native method in Java:

com/example/SimpleAdd.java :

package com.example;

public class SimpleAdd {
    static {
        System.load("/path/to/shared_librarys.so");
    }

    private static native int add(int a, int b);

    public static void main(String[] args) {
        var a = Integer.parseInt(args[0]);
        var b = Integer.parseInt(args[1]);
        System.out.println("Answer: " + add(a, b));
    }
}

SimpleAdd.zig :

const std = @import("std");
const jni = @import("jni");

pub fn add(cEnv: *jni.cEnv, _: jni.jclass, a: jni.jint, b: jni.jint) callconv(.C) jni.jint {
    return a + b;
}

root.zig :

const jni = @import("jni");

comptime {
    jni.exportJNI("com.example.SimpleAdd", @import("SimpleAdd.zig"));
}

zig-jni's People

Contributors

supericecn avatar

Stargazers

daoge avatar  avatar Koshak_Mine avatar LT_Name avatar  avatar  avatar Blackjack200 avatar  avatar Flamarine Elixirium avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.