当具有本机方法的类不在默认包中时 UnsatisfiedLinkError java 异常

发布于 2024-12-25 13:45:53 字数 1045 浏览 1 评论 0原文

我正在尝试使用 Java 中的默认 Windows 浏览器打开 URL。不幸的是,我无法使用 Desktop 类实用程序,因为代码必须与 1.5 兼容。

作为解决方案,我使用本机方法调用 ShellExecute:

public class ShellExec {
   public native int execute(String document);

   {
       System.loadLibrary("HSWShellExec");
   }

   public static void main(String args[]) throws IOException {
       new ShellExec().execute("http://www.google.com/");
   }

}

我将 DLL 文件放入 Eclipse 项目根目录中,该文件显然包含在 java.library.path 中。

如果 ShellExec 位于默认包中,一切都会完美,但如果我将其移动到任何其他包中,本机调用将失败并显示:

Exception in thread "main" java.lang.UnsatisfiedLinkError: apackage.ShellExec.execute(Ljava/lang/String;)I
at apackage.ShellExec.execute(Native Method)
at apackage.ShellExec.main(ShellExec.java:13)

任何人有任何想法为什么?我正在使用 http://www.heimetli.ch/shellexec.html 中的 DLL,

谢谢

。 .later编辑:

最终这个类和其他类将成为Eclipse RCP应用程序中的实用程序类,并且所有外部DLL将被放置在java.library.path将指向的公共lib文件夹中。可以看到 DLL,但我得到了与上面的简单示例相同类型的错误。

I am trying to open a URL with the default Windows browser, in Java. Unfortunately, I cannot use the Desktop class utilities since the code has to be compatible with 1.5.

As a solution, I am calling ShellExecute by using a native method:

public class ShellExec {
   public native int execute(String document);

   {
       System.loadLibrary("HSWShellExec");
   }

   public static void main(String args[]) throws IOException {
       new ShellExec().execute("http://www.google.com/");
   }

}

I put the DLL file in the Eclipse project root which apparently is included in java.library.path .

Everything works just perfect if ShellExec is in the default package, but if I move it in any other package, the native call fails with:

Exception in thread "main" java.lang.UnsatisfiedLinkError: apackage.ShellExec.execute(Ljava/lang/String;)I
at apackage.ShellExec.execute(Native Method)
at apackage.ShellExec.main(ShellExec.java:13)

Anybody has any ideea why? I am using the DLL from http://www.heimetli.ch/shellexec.html

Thanks

..later edit:

Eventually this class, and others, will be utility classes in an Eclipse RCP application, and all the external DLLs will be placed in a common lib folder to which the java.library.path will point to. The DLLs are seen, but I get the same type of errors as the simple example from above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟燃烟灭 2025-01-01 13:45:53

将 VM 参数 -Djava.library.path= 传递到您的项目启动配置。

pass the VM argument -Djava.library.path=<path-to-dll-folder> to your project launch configuration.

掩耳倾听 2025-01-01 13:45:53

您加载库的块对于类来说不是静态的,只是在 ShellExec 实例中定义为匿名块。由于您从未创建 ShellExec 的实例,因此永远不会调用匿名块,并且永远不会加载库。

相反,你应该这样做,

static {
   System.loadLibrary("HSWShellExec");
}

我认为这会解决你的问题。

The block you are loading the library in is not static to the class, just defined as an anonymous block in an instance of ShellExec. Since you never create an instance of ShellExec, the anonymous block never gets called and the library never gets loaded.

Instead you should have

static {
   System.loadLibrary("HSWShellExec");
}

I think that will solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文