过去几周我一直在学习防锈,并且我认为我对这种语言的使用感到满意。对于我的一个个人项目,我无法从Java调用rust代码(编译为dll)。
这是我目前用于防锈代码的内容:
use std::f64;
use std::any::Any;
#[no_mangle]
pub extern fn _Java_getDifferenceInAngleDegrees(env: *const (), jclass: *const (),from: f64, to: f64) -> f64 {
let mut angle = to - from;
// Naive algorithm
while (angle >= 180.0) {
angle -= 360.0;
}
while (angle < -180.0) {
angle += 360.0;
}
angle
}
fn main() {
println!("a");
}
这是我尝试在Java中加载它的方式
import java.io.File;
public class test{
public static native double getDifferenceInAngleDegrees(double from, double to);
public static void main(String[] args) {
File f = new File("rustMath.dll");
System.load(f.getAbsolutePath());
System.out.println(test.getDifferenceInAngleDegrees(120.0, 119.0));
}
}
我得到的错误是:
Exception in thread "main" java.lang.UnsatisfiedLinkError: test.getDifferenceInAngleDegrees(DD)D
at test.getDifferenceInAngleDegrees(Native Method)
at test.main(test.java:17)
Cargo.toml
[package]
name = "rustMath"
version = "0.1.0"
authors = ["22ethompson"]
edition = "2018"
[lib]
crate-type = ["cdylib"]
path = "src/main.rs"
[profile.release]
lto = true
[dependencies]
jni = "0.10.2"