如何使目标库可用于我的 Java 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2370545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:49:19  来源:igfitidea点击:

How do I make a target library available to my Java app?

javajna

提问by MalcomTucker

Using JNA, the documentation says:

使用 JNA,文档说:

Make your target library available to your Java program. There are two ways to do this: The preferred method is to set the jna.library.pathsystem property to the path to your target library. This property is similar to java.library.pathbut only applies to libraries loaded by JNA.

使您的目标库可用于您的 Java 程序。有两种方法可以做到这一点: 首选方法是将jna.library.path系统属性设置为目标库的路径。此属性类似于java.library.path但仅适用于 JNA 加载的库。

What does this actually mean? How do I set the jna.library.pathsystem property? My app needs to reference Kernel32.dll

这实际上意味着什么?如何设置jna.library.path系统属性?我的应用程序需要参考Kernel32.dll

Thanks

谢谢

回答by Robert Petermeier

You can set system properties by using the parameter "-D" when you invoke the Java Virtual Machine on the command line:

您可以在命令行调用 Java 虚拟机时使用参数“-D”设置系统属性:

java -Djna.library.path=<path to your library> MainClass

java -Djna.library.path=<path to your library> MainClass

You can also set this programmatically in your code at your applications's startup when it has been read from e.g. a config file:

您还可以在应用程序启动时以编程方式在代码中设置它,当它已从例如配置文件中读取时:

System.setProperty("jna.library.path", <path to your library>);
System.setProperty("jna.library.path", <path to your library>);

I haven't used JNA myself, so I don't know if it is actually too late for the JVM when you set the value in code. In that case, go with the first option.

我自己没有使用过 JNA,所以我不知道当你在代码中设置值时对 JVM 来说是否真的太晚了。在这种情况下,请选择第一个选项。