简体   繁体   English

我的 Jar 文件无法打开我的 .png 文件或我的 .wav 文件

[英]My Jar File won't open my .png files or my .wav files

I know there are several similar questions out there, but the answers for the most part seem very specific.我知道那里有几个类似的问题,但大部分答案似乎非常具体。 Anyway, my problem (as suggested by the title) is that my .jar file won't open .png files or .wav files (the only two file types I am using).无论如何,我的问题(如标题所示)是我的 .jar 文件无法打开 .png 文件或 .wav 文件(我使用的唯一两种文件类型)。 However, Netbeans will run them just fine.但是,Netbeans 可以很好地运行它们。 I am importing the pngs as ImageIcons using我使用导入 png 作为 ImageIcons

ImageIcon sun = new ImageIcon("Files\\Digital_Sun.png");

with "Files" being in my project folder. “文件”在我的项目文件夹中。 I have tried to load these files into my .jar, and when I open it with 7zip I can see that they are in there, however I seem to be unable to find the correct url for the files within the .jar.我试图将这些文件加载​​到我的 .jar 中,当我用 7zip 打开它时,我可以看到它们在那里,但是我似乎无法在 .jar 中找到正确的 url 文件。 I have tried using the .getResourceAsStream() as some others have suggested, and that doesn't work for me either.我尝试过使用 .getResourceAsStream() ,正如其他人建议的那样,但这对我也不起作用。 Perhaps I am using it wrong, but the other answers and the documentation didn't help much there.也许我用错了,但其他答案和文档在那里没有多大帮助。 Any help would be appreciated, thanks!任何帮助将不胜感激,谢谢!

Edit: Here is my attempt at using InputStreams, which hasn't helped at all either.编辑:这是我尝试使用 InputStreams 的尝试,它也没有帮助。 Can someone please show me the correct format for reading from a zip/jar file?有人可以告诉我从 zip/jar 文件中读取的正确格式吗?

InputStream sunIS = new BufferedInputStream( new FileInputStream("Files\\Digital_Sun.png"));
InputStream overIS = new BufferedInputStream( new FileInputStream("Files\\overcast_digital.png"));
InputStream rainIS = new BufferedInputStream( new FileInputStream("Files\\rain_digital.png"));
InputStream snowIS = new BufferedInputStream( new FileInputStream("Files\\snow_digital.png"));
InputStream fairIS = new BufferedInputStream( new FileInputStream("Files\\digital_fair.png"));
InputStream clearIS = new BufferedInputStream( new FileInputStream("Files\\clear_digital.png"));
InputStream windyIS = new BufferedInputStream( new FileInputStream("Files\\windy_digital.png"));
InputStream nullIS = new BufferedInputStream( new FileInputStream("Files\\weather_unknown.png"));

ImageIcon sun;
ImageIcon overcast;
ImageIcon rain; 
ImageIcon snow;
ImageIcon fair;
ImageIcon clear;
ImageIcon windy;
ImageIcon nullWeather;

public DigitalClock() throws IOException, LineUnavailableException, URISyntaxException { //clock constructor
    super();
    sun = new ImageIcon(ImageIO.read(sunIS));
    overcast = new ImageIcon(ImageIO.read(overIS));
    rain = new ImageIcon(ImageIO.read(rainIS));
    snow = new ImageIcon(ImageIO.read(snowIS));
    fair = new ImageIcon(ImageIO.read(fairIS));
    clear = new ImageIcon(ImageIO.read(clearIS));
    windy = new ImageIcon(ImageIO.read(windyIS));
    nullWeather = new ImageIcon(ImageIO.read(nullIS));

I solved it, the following website gives a great explanation for using .getResourceAsStream()我解决了,下面的网站对使用 .getResourceAsStream() 给出了很好的解释

http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM