当 Java 运行时位于可移动驱动器上时,如何防止 NoClassDefFoundError?

发布于 2025-01-03 16:39:25 字数 300 浏览 1 评论 0原文

我在 USB 记忆棒上有一个 Java 应用程序以及 JRE(Windows、Mac OSX 和 Linux 各一个),这样即使未安装 Java,它也可以在任何系统上运行。我正在运行一个线程来检测 USB 记忆棒是否被移除,如果是则退出应用程序。

我想让它在退出之前显示一条消息(或告诉他们重新插入驱动器),但如果我尝试显示 JOptionPane,我会收到 NoClassDefFoundError,因为 JRE 已与 USB 一起删除。

有没有办法将所需的类保留在内存中,以便 Java 不会尝试从不再存在的文件系统加载它们?

谢谢!

I have a Java application on a USB stick along with the JRE (one for each Windows, Mac OSX and Linux) so that it can be run on any system even when Java is not installed. I have a thread running to detect if the USB stick is removed and if it is it exits the application.

I want to have it display a message before exiting (or tell them to re-insert the drive) but if I try to dispaly a JOptionPane I get the NoClassDefFoundError because the JRE has been removed along with the USB.

Is there a way to keep the needed classes in memory so that Java does not try to load them from the filesystem that is no longer present?

Thanks!

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

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

发布评论

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

评论(1

风吹雨成花 2025-01-10 16:39:25

对于大多数运行时,加载的任何类都将位于 RAM 中,即 Java 内存的 PermGen 部分。

在我看来,可能发生的情况是显示错误需要以前未加载过的类 - 这些类仅在不再存在的驱动器上可用。

您可以通过在程序启动时触发错误显示逻辑来解决此问题,除非将其设置为不可见的显示模式。这应该使用相同的代码路径,以便加载所有必需的类,并确保当您需要显示错误时它们是已知的。如果这太困难,您可以手动调用 Class.forName("javax.swing.JOptionPane") (对于每个所需的类),尽管这更脆弱,并且如果您更改,可能会损坏您的渲染代码无需更新要加载的硬编码类。

如果情况并非如此(即之前已加载所有必需的类,但问题仍然存在),那么您的运行时显然正在卸载类。您必须查看其文档才能了解它在这里做什么以及如何停止它。

For most runtimes, any classes that are loaded will be in RAM, in the PermGen section of Java's memory.

To my mind what's likely happening is that displaying the error requires classes that haven't been loaded before - which are only available on the drive that's no longer there.

You might be able to work around this by triggering your error displaying logic when the program starts up, except have it in a mode where it's displayed invisibly. This should exercise the same code path such that all the required classes are loaded, and ensure that they're already known when you need to display your error. If this is too difficult, you could just manually call Class.forName("javax.swing.JOptionPane") instead (for each class required), though this is more brittle and likely to break if you change your rendering code without updating the hard-coded classes to load.

If this isn't the case (i.e. all the required classes have previously been loaded, and you still have the problem) then your runtime is obviously unloading classes. You'd have to look at its documentation to see what it's doing here, and how to stop it.

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