为什么加载库 JNA 的 2 路径
Why 2 Path for load Library JNA
你好这是我的代码:
if (isWindows()) {
//System.setProperty("jna.library.path", getClass().getResource("/resources/win32-x86").getPath());//netbeans WinOs
System.setProperty("jna.library.path", System.getProperty("user.dir").toString()+File.separator+"Desktop");//compiler WinOs
} else if (isMac()) {
//System.setProperty("jna.library.path", getClass().getResource("/resources").getPath());//netbeans MacOs
System.setProperty("jna.library.path", System.getProperty("user.dir").toString()+File.separator+"Desktop");//compiler MacOs
} else {
System.out.println("Your OS is not support!!");
}
为什么我有 2 个 PATH(不明白,因为要添加图像我只有一个路径)OS,一个与 IDE 一起使用,另一个与 .JAR 一起使用?
我刚刚意识到,当我使用 windows 和 运行 项目(来自 netbeans)时 "Library" 加载并获得信息,但是当我编译时然后我启动我的 .jar 我得到错误:
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: %1 is not a valid Win32 application.
My Structure
正确吗?
On mac 仅适用于此命令:java -jar "/System/Volumes/Data/Users/hugoclo/NetBeansProjects/Prezauto/dist/Prezauto.jar"
自终端。如果单击 jar 我有消息错误:未找到 .....
对不起我的英语,
"why" 可能有两个原因。虽然 Java 是跨平台的,但 JNA(依赖于某些本机代码)在不同操作系统上的行为必然不同。特别是在加载 DLL (Windows) 或动态库 (OSX) 的情况下,您不想混搭。因为可能为不同的操作系统编译同名的 dll,JNA 的 Getting Started 页面标识了这些库的标准位置:
Make your target library available to your Java program. There are several 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.
- Change the appropriate library access environment variable before launching the VM. This is
PATH
on Windows, LD_LIBRARY_PATH
on Linux, and DYLD_LIBRARY_PATH
on OSX.
- Make your native library available on your classpath, under the path
{OS}-{ARCH}/{LIBRARY}
, where {OS}-{ARCH}
is JNA's canonical prefix for native libraries (e.g. win32-x86, linux-amd64, or darwin). If the resource is within a jar file it will be automatically extracted when loaded.
在您的代码中,您似乎正在尝试执行第一个选项(设置 jna.library.path
)以包含用户的桌面。这对测试很好,对生产不利,并且可能是您编译的 jar 找不到它的原因。此外,通过设置此变量,您将覆盖它之前的任何(默认)位置。如果你想走这条路,你应该复制保存的位置,然后附加你自己的附加路径。
但是,对于要分发给用户的代码,您不想依赖绝对文件路径。最好将库放在标准的相对路径位置:resources
路径(如果使用 Maven,则为 src/main/resources
),执行时将在您(或任何用户)的类路径中可用。这似乎与代码中注释掉的 Windows 分支一致,该分支将查找 resources
文件夹的 win32-x86
子目录。
你可能已经告诉你的 IDE 添加一些东西到类路径(所以它在那里工作)但是如果它不在标准位置,它可能会在 jar 中失败。
我不确定为什么你的代码的 macOS 分支没有将资源放在 darwin
子目录中,但它可能应该。
你好这是我的代码:
if (isWindows()) {
//System.setProperty("jna.library.path", getClass().getResource("/resources/win32-x86").getPath());//netbeans WinOs
System.setProperty("jna.library.path", System.getProperty("user.dir").toString()+File.separator+"Desktop");//compiler WinOs
} else if (isMac()) {
//System.setProperty("jna.library.path", getClass().getResource("/resources").getPath());//netbeans MacOs
System.setProperty("jna.library.path", System.getProperty("user.dir").toString()+File.separator+"Desktop");//compiler MacOs
} else {
System.out.println("Your OS is not support!!");
}
为什么我有 2 个 PATH(不明白,因为要添加图像我只有一个路径)OS,一个与 IDE 一起使用,另一个与 .JAR 一起使用?
我刚刚意识到,当我使用 windows 和 运行 项目(来自 netbeans)时 "Library" 加载并获得信息,但是当我编译时然后我启动我的 .jar 我得到错误:
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: %1 is not a valid Win32 application.
My Structure
正确吗?
On mac 仅适用于此命令:java -jar "/System/Volumes/Data/Users/hugoclo/NetBeansProjects/Prezauto/dist/Prezauto.jar"
自终端。如果单击 jar 我有消息错误:未找到 .....
对不起我的英语,
"why" 可能有两个原因。虽然 Java 是跨平台的,但 JNA(依赖于某些本机代码)在不同操作系统上的行为必然不同。特别是在加载 DLL (Windows) 或动态库 (OSX) 的情况下,您不想混搭。因为可能为不同的操作系统编译同名的 dll,JNA 的 Getting Started 页面标识了这些库的标准位置:
Make your target library available to your Java program. There are several 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 tojava.library.path
, but only applies to libraries loaded by JNA.- Change the appropriate library access environment variable before launching the VM. This is
PATH
on Windows,LD_LIBRARY_PATH
on Linux, andDYLD_LIBRARY_PATH
on OSX.- Make your native library available on your classpath, under the path
{OS}-{ARCH}/{LIBRARY}
, where{OS}-{ARCH}
is JNA's canonical prefix for native libraries (e.g. win32-x86, linux-amd64, or darwin). If the resource is within a jar file it will be automatically extracted when loaded.
在您的代码中,您似乎正在尝试执行第一个选项(设置 jna.library.path
)以包含用户的桌面。这对测试很好,对生产不利,并且可能是您编译的 jar 找不到它的原因。此外,通过设置此变量,您将覆盖它之前的任何(默认)位置。如果你想走这条路,你应该复制保存的位置,然后附加你自己的附加路径。
但是,对于要分发给用户的代码,您不想依赖绝对文件路径。最好将库放在标准的相对路径位置:resources
路径(如果使用 Maven,则为 src/main/resources
),执行时将在您(或任何用户)的类路径中可用。这似乎与代码中注释掉的 Windows 分支一致,该分支将查找 resources
文件夹的 win32-x86
子目录。
你可能已经告诉你的 IDE 添加一些东西到类路径(所以它在那里工作)但是如果它不在标准位置,它可能会在 jar 中失败。
我不确定为什么你的代码的 macOS 分支没有将资源放在 darwin
子目录中,但它可能应该。