1

Is there any way to verify at compile time that a resource referenced in source code has been included in an output JAR?

I am creating a Java JAR file that contains several PNGs. I load them like this:

ImageIcon imageIcon = new ImageIcon(MyClass.class.getResource("/path/to/resource.png");

Currently I don't find out about missing resources until I get exceptions at run time. I don't want a static list of resources in my build script, I want the compiler to figure out which resources I'm using in the code and check that they exist.

4

1 回答 1

0

实际上,仅在运行时查找路径中的错误似乎是浪费时间。

另一件事是,在移动文件后,由于某种原因,可能无法立即在 .jar 中看到资源。我需要在它们变得可见之前做一些魔术(重建项目,重新创建 jar)。

这个想法可能是在编译之前基于文件系统生成一个带有常量的java类(例如ResourceManager)。

然后使用它们来访问资源:

就像是:

ResourceManager.GET_CUSTOMER_SQL_PATH

你怎么看?

于 2019-05-16T04:09:54.210 回答