为什么我的图像没有出现在可执行jar中,即使它们出现在Eclipse中? [重复]

问题描述 投票:1回答:2

这个问题在这里已有答案:

我正在制作游戏,我需要显示游戏的图像才能玩。为了显示这些图像,我使用ImageIO.read,当我在Eclipse中运行代码时,所有图像都显示正常。我使用的图像作为我的主类在一个单独的文件夹中,但我将此文件夹设置为构建路径中的源文件夹。

我试图使用getClass().getClassLoader().getResource("image.jpg")以及main.class.getResource("image.jpg"),我试图编写一个/image.jpg而不是Runnable Jar文件,我试图提取库并打包库,所有这些东西在Eclipse中运行良好但不是在JAR文件中......

我目前的代码是这样的:

File prisonCellFile = new File(main.class.getResource("cellule.jpg").getURI());               

prisonCell = ImageIO.read(prisonCellFile);

除了图像之外,其他一切在jar文件中运行正常,我看到图像被导出到jar文件中,但它们只是没有出现......你知道我做错了什么吗?

java image executable-jar embedded-resource javax.imageio
2个回答
-1
投票

感谢Andrew Thompson的评论,我终于得到了它的工作。我改变了:

 File prisonCellFile = new File(main.class.getResource("cellule.jpg").getURI());

成:

URL prisonCellFile = main.class.getResource("cellule.jpg");

-2
投票

你需要使用:

File prisonCellFile = new main.class.getResource("cellule.jpg").getFile());

© www.soinside.com 2019 - 2024. All rights reserved.