【问题标题】:src folder not included in exported .jar messing up resource paths (Eclipse)src 文件夹未包含在导出的 .jar 中,弄乱了资源路径(Eclipse)
【发布时间】:2015-12-17 06:12:04
【问题描述】:

所以我有一个可以在 Eclipse 中完美运行的项目,但是当我将它导出为可运行的 .jar 时,我的 .png 资源文件的路径将不再起作用,因为 src 文件夹不包含在导出的 .jar 中。

createTexture("resources/grass.png", 0);

这是我使用路径的地方。

public static void createTexture(String path, int index){
    try (InputStream inputStream = Drawer.class.getClassLoader().getResourceAsStream(path)){
        textureDecoder = new PNGDecoder(inputStream); [...]

现在我得到了错误

Exception in thread "main" java.lang.NullPointerException
at de.matthiasmann.twl.utils.PNGDecoder.readFully(PNGDecoder.java:802)
at de.matthiasmann.twl.utils.PNGDecoder.<init>(PNGDecoder.java:112)
at workers.Drawer.createTexture(Drawer.java:80)
at workers.Drawer.openglSetup(Drawer.java:44)
at jonta.Main.setUp(Main.java:41)
at jonta.Main.main(Main.java:121)

旧: 这显然是我尝试运行 .jar 时遇到的错误:

java.io.FileNotFoundException: src\resources\grass.png

如果相关的话,我当然会包含更多我所做的代码/配置。

编辑:问题已解决,谢谢!

【问题讨论】:

  • jar 中的对象不是文件,不能使用 File API 访问。

标签: java eclipse jar path src


【解决方案1】:

用途:

InputStream inputStream = className.class.getClassLoader().getResourceAsStream(path);

【讨论】:

  • 我知道我不能在静态上下文中使用 this
【解决方案2】:

在 Eclipse 中,选择“Export ...”和“JAR File”后,有“Export Java source files and resources”复选框。您需要单击该复选框。

【讨论】:

  • 没有改变任何东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-29
  • 2014-10-20
  • 2017-07-31
  • 1970-01-01
  • 2018-04-16
  • 2014-07-20
  • 1970-01-01
相关资源
最近更新 更多