- Notifications
You must be signed in to change notification settings - Fork38
drrb/java-rust-example
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
An example project showing how to call into Rust code from Java.
OSX | Linux | Windows |
---|---|---|
- Java 7+
- Rust (tested with 1.0, nightly)
So far, the project contains
- Rust code and Java code
- A Java interface to the Rust code, usingJNA
- A script to build the Rust code into a library and put it on the classpath where JNA can find it
- Examples of passing strings, structs, and callback functions between Java and Rust
The best place to start looking at the examples is in the test code(GreetingsTest.java).The test contains lots of executable examples of calling into Rust code fromJava. From the test, you can navigate to theJava codeand theRust code. Theimplementation is heavily commented to explain it.
So far, it contains examples of the following (click the links to see!):
- Arguments: passing simple arguments from Java to Rust (Java side /Rust side)
- Return values: returning simple values from Rust to Java (Java side /Rust side)
- Struct arguments: passing structs to Rust from Java (Java side /Rust side)
- Returning structs (2 examples): returning structs from Rust by value and by reference (Java side /Rust side)
- Callbacks (3 examples): passing callbacks to Rust that get called from the Rust code (Java side /Rust side)
- Freeing memory: freeing memory allocated in Rust (Java side /Rust side)
To build the project, and run the tests, use Maven. This will build a jarcontaining the Rust code and the Java code. This assumes you have Rustinstalled, and on the path.
$ mvn package
You can then run the jar that is produced to see the integration work.
$ java -jar target/greeter.jar JohnHello from Rust, John
This project is tested on OSX, Ubuntu, and Windows. It should also work on any 32 bit or 64 bit Gnu/Linux system.
Some of the examples leak memory. Any memory that is allocated in Rust needs to be freed manually because it's not managed by JNA. Some examples pass objects back into Rust to be dropped for this reason, but we don't clean up everything properly (strings, for example). This is almost certainly not a limitation of Rust, but a limitation of my current understanding of Rust.
Java/Rust ExampleCopyright (C) 2015 drrb
This program is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.
This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.
You should have received a copy of the GNU General Public Licensealong with this program. If not, seehttp://www.gnu.org/licenses/.
About
Embedding Rust in Java
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.