public class Main {
public static void main(String[] args) {
// 相当于 Math.sqrt(4)
var sqrt = Math.class.getMethod("sqrt", double.class);
double sqrt4 = sqrt.invoke(null, 4);
System.out.println(sqrt4); // ==> 2
}
}
配置 reflect-config.json
[
{
"name": "java.lang.Math",
"methods": [
{ "name": "sqrt", "parameterTypes": ["double"] }
]
}
]
用命令行参数-H:ReflectionCongurationFiles=<>
注入就行了
native-image -H:ReflectionConfigurationFiles=<JSON配置文件> <字节码文件>