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

问题描述:

文档使用JNA:

使目标库可用于Java程序.有两种方法可以执行此操作:首选方法是将jna.library.path系统属性设置为目标库的路径.此属性类似于java.library.path,但仅适用于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.path system property to the path to your target library. This property is similar to java.library.path but only applies to libraries loaded by JNA.

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

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

谢谢

在命令行上调用Java虚拟机时,可以通过使用参数"-D"来设置系统属性:

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

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>);

我自己还没有使用过JNA,所以当您在代码中设置值时,我不知道对于JVM实际上是否为时已晚.在这种情况下,请选择第一个选项.

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.