java - 在不生成 Jar 的情况下从 Eclipse 调试 SplashScreen

标签 java eclipse debugging swing splash-screen

我在网上搜索过,但找不到这个问题的答案:

我需要调试根据您正在访问的模块更改 SplashScreen 的应用程序的功能。

我知道代码:

SplashScreen splash = SplashScreen.getSplashScreen();

可用于在您通过以下任一时获取实例:

  • 命令行启动:java -splash:path/image.gif ClassFile
  • list 中的初始图像:splashscreen-image: img/SplashNomina.gif

当我尝试通过在 Eclipse 中从 VM args 传递 -splash 值来运行应用程序时,它仍然不起作用。

这是否真的可能,因为 SplashScreen.getSplashScreen() 始终为 NULL。 我一直在尝试通过但没有成功:

  • -splash:image.gif
  • -Dsplash=image.gif

现在我看到这个 Splash api 有很多限制,因为它总是需要传递一个参数。我认为能够在运行时传递参数会更加灵活:(

非常感谢任何帮助!

最佳答案

好吧,这也困扰着我。

我构建了一个可运行的 jar 带有 list 条目

SplashScreen-Image: MyGraphic.jpg

它按预期工作。

从 Eclipse 中,将 VM arg 指定为

-splash:MyGraphic.jpg

没有这样的运气

SplashScreen.getSplashScreen() 返回 null。

原因是 JDK(至少 1.6)中 SplashScreen.getSplashScreen() 的脑残实现。我认为。如果不了解 native 代码在做什么,就很难说清楚。但是,这是来自 java.awt.SplashScreen 的这个方法。我不确定它是否被调用,但研究它确实为我提供了在 Eclipse 中运行它所需的基本线索:

public synchronized URL getImageURL() throws IllegalStateException {
    checkVisible();
    if (imageURL == null) {
        try {
            String fileName = _getImageFileName(splashPtr);
            String jarName = _getImageJarName(splashPtr);
            if (fileName != null) {
                if (jarName != null) {
                    imageURL = new URL("jar:"+(new File(jarName).toURL().toString())+"!/"+fileName);
                } else {
                    imageURL = new File(fileName).toURL();
                }
            }
        }
        catch(java.net.MalformedURLException e) {
            // we'll just return null in this case
        }
    }
    return imageURL;
}

请注意,对于文件(即命令行而不是 jar 启动),它不是执行 getResource() 来获取 URL,而是打开一个与 CWD 相关的文件。由于 Eclipse 运行配置默认从项目的根目录运行,答案是将路径指定为相对路径,而不是期望类路径查找。

因此,由于我是用 maven 构建的,所以我的图像位于 src/main/resources/MyGraphic.jpg。将其指定为命令行参数:即

-splash:src/main/resources/MyGraphic.jpg 

允许它在 Eclipse 中工作(或者,我猜,任何命令行)

我不确定这是为什么,因为 getSplashScreen() 未调用 getImageURL 方法,但它确实有效。

对我来说,这对于 Sun/Oracle 来说有点脑残。他们可以很容易地用类似的东西完成类路径查找 imageURL = getResource(filename) 但他们没有。

简短的回答是启动画面命令行语法指的是相对于当前工作目录的文件名,而不是相对于类路径的文件名。

关于java - 在不生成 Jar 的情况下从 Eclipse 调试 SplashScreen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6906267/

相关文章:

java - Hamcrest 匹配器比较来自 JSON 的 double 值

c++ - 如何调试堆损坏错误?

ruby-on-rails - 尝试启动服务器时发生 Rails LoadError

c - 字节到字节加密 "C"程序逻辑错误

java - JList 项目删除后回来?

java - Soap 网络服务请求与 HTTP POST 请求?

java - 我如何控制另一个类中的构造函数访问

java - 如何使用 Dagger ?

eclipse - Eclipse 的免费 JSP 插件?

java - 操作系统 : ant in Eclipse doesn't find mvn - but works in terminal