Java Applet - 如何确定客户端计算机上是否存在外部 jar 文件

发布于 2024-12-08 09:13:35 字数 507 浏览 2 评论 0原文

下午好,

我正在寻找一种可靠的方法来确定客户端计算机上是否存在外部 jar,谁将使用我的 java 小程序。现在我正在做的是伪代码,

try 
{
  controller.init()
}
catch Exception(Jar does not exist)
{
  make pretty screen to tell users to download setup file
  fire up browser with link to setup file
}

但是向我抛出的异常是 UnsatisfiedLinkError ,我无法捕获它,因为它不是可恢复的。这基本上意味着我“离开”了一个浏览器窗口,其中包含指向安装文件的链接,但没有解释它为何出现。

当然,“简单”的修复只是在托管小程序的 html 页面中添加“如果 Java 崩溃,请下载建议的安装文件”,但是我更喜欢一个编程解决方案,它就在这里你们也参与进来了:)

提前感谢您的所有回答。

/杰布

Good afternoon

I'm in the search of a solid way to determine if an external jar exist on the client machine, who're going to use my java applet. Right now what I'm doing is this in pseudo code

try 
{
  controller.init()
}
catch Exception(Jar does not exist)
{
  make pretty screen to tell users to download setup file
  fire up browser with link to setup file
}

However the exception that is thrown at me is UnsatisfiedLinkError which I can't catch as it's not something that's recoverable. This basically means I'm "left" with a browser window with the link to the setup file, but no explanation as to why it has come up.

Of course an "easy" fix is just to add to the html page that hosts the applet that "If Java blows up, please just download the setup file that is suggested", however I would much rather like a programmatical solution, and it's here you guys and gals come into the picture :)

Thanks in advance for any and all answers.

/Jeb

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

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

发布评论

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

评论(1

终难愈 2024-12-15 09:13:35

您可以检查类路径中是否存在该 jar (System.getProperty("java.class.path"))。

另一种方法是查看是否可以加载类文件资源。假设您要加载com.thirdparty.SomeClass。然后,在调用 controller.init() 的任何类中,您都可以尝试

this.getClass().getCloassLoader.findResource("com.thirdparty.SomeClass.class")

this.getClass().getClassLoader().getResource("com.thirdparty.SomeClass.class")

如果返回值不为 null,则类加载应该会成功。

当然,jar 中的类本身可能依赖于其他类......您必须决定如何处理这种情况。

You can check whether the jar exists in the classpath (System.getProperty("java.class.path")).

Another approach is to see if you can load the class file resource. Say you are going to load com.thirdparty.SomeClass. Then, in whichever class that is calling controller.init() you can try

this.getClass().getCloassLoader.findResource("com.thirdparty.SomeClass.class")

this.getClass().getClassLoader().getResource("com.thirdparty.SomeClass.class")

If the return value is not null then the class loading should hopefully succeed.

Of course, the classes in jar may themselves depend on other classes...you have to decide how you want to handle that situation.

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