简体   繁体   English

为什么 2 加载库 JNA 的路径

[英]Why 2 Path for load Library JNA

Hello This is my code:你好这是我的代码:

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!!");
        }

Why I have 2 PATH (don't understand because for add an image i have only one Path) by OS, one for use with IDE and another for use with.JAR?为什么我有 2 个 PATH(不明白,因为要添加图像,我只有一个路径),一个用于 IDE,另一个用于.JAR?

I just realized, that when I'm use windows and I run the project (from netbeans) the "Library" load and I get the information, but when I compile and I launch my.jar I get error:我刚刚意识到,当我使用 windows 并运行项目(来自 netbeans)时,“库”加载并获得了信息,但是当我编译并启动 my.jar 时出现错误:

Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: %1 is not a valid Win32 application.

My Structure我的结构

It is correct?它是正确的?

On mac only work with this command: java -jar "/System/Volumes/Data/Users/hugoclo/NetBeansProjects/Prezauto/dist/Prezauto.jar" since Terminal.在 Mac 上,只能使用以下命令: java -jar "/System/Volumes/Data/Users/hugoclo/NetBeansProjects/Prezauto/dist/Prezauto.jar"从终端开始。 If click on jar i have message error: Not Found..... Sorry about my English,如果单击 jar 我有消息错误:未找到.....对不起我的英语,

There can be two reasons for the "why". “为什么”可能有两个原因。 While Java is cross-platform, JNA (which relies on some native code) must necessarily behave differently on different operating systems.虽然 Java 是跨平台的,但 JNA(依赖于一些本机代码)在不同的操作系统上必须表现得不同。 Particularly in the case of loading DLLs (Windows) or dynamic libraries (OSX), you don't want to mix and match.特别是在加载 DLL (Windows) 或动态库 (OSX) 的情况下,您不想混搭。 Because it might be possible to have a dll with the same name compiled for different operating systems, JNA's Getting Started page identifies standard locations for these libraries:因为可能会为不同的操作系统编译具有相同名称的 dll,所以 JNA 的入门页面确定了这些库的标准位置:

Make your target library available to your Java program.使您的目标库可用于您的 Java 程序。 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.首选方法是将jna.library.path系统属性设置为目标库的路径。 This property is similar to java.library.path , but only applies to libraries loaded by JNA.此属性类似于java.library.path ,但仅适用于 JNA 加载的库。
  • Change the appropriate library access environment variable before launching the VM.在启动 VM 之前更改适当的库访问环境变量。 This is PATH on Windows, LD_LIBRARY_PATH on Linux, and DYLD_LIBRARY_PATH on OSX.这是 Windows 上的PATH ,Linux 上的LD_LIBRARY_PATH和 OSX 上的DYLD_LIBRARY_PATH
  • 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 (eg win32-x86, linux-amd64, or darwin).使您的本机库在您的类路径中可用,路径为{OS}-{ARCH}/{LIBRARY} ,其中{OS}-{ARCH}是 JNA 的本机库规范前缀(例如 win32-x86、linux-amd64 或达尔文)。 If the resource is within a jar file it will be automatically extracted when loaded.如果资源在 jar 文件中,加载时将自动提取。

In your code, you appear to be trying to do the first option (setting the jna.library.path ) to include the user's desktop.在您的代码中,您似乎正在尝试执行第一个选项(设置jna.library.path )以包含用户的桌面。 That's fine for testing, not good for production, and likely the reason your compiled jar can't find it.这对测试很好,对生产不利,可能是您编译的 jar 找不到它的原因。 Furthermore, by setting this variable, you are overwriting any previous (default) location for it.此外,通过设置此变量,您将覆盖它之前的任何(默认)位置。 If you want to go this route, you should copy the saved location and then append your own additional path to it.如果你想 go 这条路线,你应该复制保存的位置然后 append 你自己的附加路径到它。

However, for code you'll distribute to users, you don't want to have to rely on an absolute file path.但是,对于您将分发给用户的代码,您不希望必须依赖绝对文件路径。 It's far better to put the library in a standard relative path location: a resources path ( src/main/resources if using Maven) that will be available on your (or any user's) classpath when executing.将库放在标准的相对路径位置要好得多: resources路径(如果使用 Maven,则为src/main/resources ),在执行时将在您(或任何用户的)类路径中可用。 This seems to align with the commented-out Windows branch of your code, which will look in the win32-x86 subdirectory of your resources folder.这似乎与代码的注释掉的 Windows 分支一致,它将在resources文件夹的win32-x86子目录中查找。

You may have told your IDE to add something to the classpath (so it works there) but if it's not in a standard location, it may fail in a jar.您可能已经告诉您的 IDE 在类路径中添加一些内容(因此它可以在那里工作),但如果它不在标准位置,它可能会在 jar 中失败。

I'm not sure why the macOS branch of your code does not put the resources in the darwin subdirectory but it probably should.我不确定为什么代码的 macOS 分支没有将资源放在darwin子目录中,但它可能应该。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM