Skip to content

Add oxbitnet-java crate — Java/JNI bindings#11

Merged
m96-chan merged 1 commit into
mainfrom
feat/java
Feb 20, 2026
Merged

Add oxbitnet-java crate — Java/JNI bindings#11
m96-chan merged 1 commit into
mainfrom
feat/java

Conversation

@m96-chan

Copy link
Copy Markdown
Owner

Summary

  • Add oxbitnet-java crate with JNI native methods wrapping oxbitnet directly (same pattern as oxbitnet-python with PyO3)
  • Java API: BitNet.loadSync(), generate(), chat() with TokenCallback streaming, AutoCloseable support
  • Includes Java source files (io.github.m96chan.oxbitnet package), Gradle build stub, Chat.java example, and crate README

Closes #5

Details

Rust side (src/lib.rs):

  • JavaBitNet struct owns Option<BitNet> + tokio::Runtime (same as Python)
  • JNI functions: nativeLoad, nativeGenerate, nativeChat, nativeFree
  • Progress callback via GlobalRef + JavaVM::attach_current_thread
  • Token callback: boolean onToken(String) — return false to stop early
  • Errors thrown as OxBitNetException via env.throw_new()

Java side (7 files):

  • BitNet — main class, AutoCloseable, stores long nativeHandle
  • ChatMessage — record (role, content)
  • GenerateOptions — builder pattern (maxTokens, temperature, topK, repeatPenalty, repeatLastN)
  • TokenCallback@FunctionalInterface
  • LoadOptions — progress callback + cache dir
  • LoadProgress — record for progress info
  • OxBitNetException — RuntimeException subclass

Test plan

  • cargo build -p oxbitnet-java compiles to .so
  • cargo clippy -p oxbitnet-java -- -D warnings passes clean
  • Compile and run Chat.java example with a GGUF model
  • Verify: loads model, streams tokens via callback, clean shutdown

🤖 Generated with Claude Code

Wraps oxbitnet directly via the `jni` crate (same pattern as
oxbitnet-python with PyO3). Provides BitNet.loadSync(), generate(),
chat() with TokenCallback streaming, and AutoCloseable support.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@m96-chan m96-chan merged commit 1fbfcb4 into main Feb 20, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Java bindings (oxbitnet-java)

1 participant