{"id":25939360,"url":"https://github.com/hellyguo/rust_java_samples","last_synced_at":"2025-03-04T04:16:49.770Z","repository":{"id":248459436,"uuid":"828741736","full_name":"hellyguo/rust_java_samples","owner":"hellyguo","description":"rust-java samples","archived":false,"fork":false,"pushed_at":"2024-07-29T05:34:30.000Z","size":113,"stargazers_count":10,"open_issues_count":0,"forks_count":6,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-07-29T07:01:22.674Z","etag":null,"topics":["demo","demos","example","examples","java","rust","sample","samples"],"latest_commit_sha":null,"homepage":"","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/hellyguo.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-15T03:19:24.000Z","updated_at":"2024-07-29T05:34:33.000Z","dependencies_parsed_at":"2024-07-29T07:16:28.969Z","dependency_job_id":null,"html_url":"https://github.com/hellyguo/rust_java_samples","commit_stats":null,"previous_names":["hellyguo/rust_java_samples"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellyguo%2Frust_java_samples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellyguo%2Frust_java_samples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellyguo%2Frust_java_samples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellyguo%2Frust_java_samples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hellyguo","download_url":"https://codeload.github.com/hellyguo/rust_java_samples/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241780477,"owners_count":20019061,"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":["demo","demos","example","examples","java","rust","sample","samples"],"created_at":"2025-03-04T04:16:49.293Z","updated_at":"2025-03-04T04:16:49.752Z","avatar_url":"https://github.com/hellyguo.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rust-java samples\n\n[Chinese version, 中文版](README_cn.md)\n\n## desc\n\nThis repository contains sample `Rust` programs that call `Java` methods, and `Java` classes that call `Rust` methods.\n\n## sample list\n\n0. **sample000**, call `Rust` method from `Java`\n\n   - [s000_hello.rs](sample/src/samples/s000_hello.rs)\n   - [HelloWorld.java](sample4j/src/main/java/sample/s000/HelloWorld.java)\n\n1. **sample001**, convert `Java` primitive types to bytes in `Rust`\n\n   - [s001_bytes.rs](sample/src/samples/s001_bytes.rs)\n   - [Bytes.java](sample4j/src/main/java/sample/s001/Bytes.java)\n\n2. **sample002**, accept a `Java` object in `Rust`, and callback to `Java`\n\n   - [s002_callback.rs](sample/src/samples/s002_callback.rs)\n   - [Caller.java](sample4j/src/main/java/sample/s002/Caller.java)\n   - [s002_callback_fast.rs](sample/src/samples/s002_callback_fast.rs)\n   - [Caller2.java](sample4j/src/main/java/sample/s002/Caller2.java)\n\n3. **sample003**, reverse `Java` bytes\n\n   - [s003_reverse_bytes.rs](sample/src/samples/s003_reverse_bytes.rs)\n   - [ReverseBytes.java](sample4j/src/main/java/sample/s003/ReverseBytes.java)\n\n4. **sample004**, access `Java` DirectBuffer in `Rust`\n\n   - [s004_direct_buf.rs](sample/src/samples/s004_direct_buf.rs)\n   - [DirectBuf.java](sample4j/src/main/java/sample/s004/DirectBuf.java)\n\n5. **sample005**, access `Java` primitive array in `Rust`\n\n   - [s005_primitive_array.rs](sample/src/samples/s005_primitive_array.rs)\n   - [PrimitiveArray.java](sample4j/src/main/java/sample/s005/PrimitiveArray.java)\n\n6. **sample006**, create `Java` object in `Rust`\n\n   - [s006_create_object.rs](sample/src/samples/s006_create_object.rs)\n   - [ObjectCreator.java](sample4j/src/main/java/sample/s006/ObjectCreator.java)\n\n7. **sample007**, set a field to `Java` object in `Rust`\n\n   - [s007_fill_field.rs](sample/src/samples/s007_fill_field.rs)\n   - [FieldFiller.java](sample4j/src/main/java/sample/s007/FieldFiller.java)\n\n8. **sample008**, load `Java` class in `Rust`\n\n   - [s008_load_class.rs](sample/src/samples/s008_load_class.rs)\n   - [s008_class_buf.rs](sample/src/samples/s008_class_buf.rs)\n   - [MemClassLoader.java](sample4j/src/main/java/sample/s008/MemClassLoader.java)\n\n   \u003e `s008_class_buf.rs` is generated by [java2u8vec.sh](shell/java2u8vec.sh)\n\n9. **sample009**, register `Java` method dynamically in `Rust`\n\n   - [s009_reg_method.rs](sample/src/samples/s009_reg_method.rs)\n   - [NativeMethodRegister.java](sample4j/src/main/java/sample/s009/NativeMethodRegister.java)\n\n[more ..](doc/sample_list.md)\n\n## build\n\n```shell\ncargo build --release\n```\n\n`cargo` will generate `.so` in `target/release`, and call `build_java.sh` to build `.jar`, which contains `.so` in `resources` folder.\n\n## how-to\n\n1. call `Java` each JUnit test in IDE\n2. call `Java` JUnit test suite in IDE\n\n\u003e **Note**: need to setup `JVM` args: `-DsampleLib=\u003cpath to lib\u003e`\n\nor\n\n1. execute `Java` each JUnit test in console\n\n    ```shell\n    gradle test --tests HelloWorldTest\n    ```\n\n2. execute `Java` each JUnit test suite in console\n\n    ```shell\n    gradle test --tests SamplesSuite\n    ```\n\nThe [JUnit report](sample4j/build/reports/tests/test/index.html) will be generated.\n\n## thanks\n\nThanks to [metaworm](https://github.com/metaworm)([rust-java-demo](https://github.com/metaworm/rust-java-demo)). This article ([url1](https://zhuanlan.zhihu.com/p/568062165)/[url2](https://rustcc.cn/article?id=4ca84a67-d972-4460-912e-a297ec5edc0a)) is the most effective and detailed for learning how to call `JNI` through `Rust`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhellyguo%2Frust_java_samples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhellyguo%2Frust_java_samples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhellyguo%2Frust_java_samples/lists"}