问题描述
URL logoPath = new MainApplication( ).getClass()getClassLoader()的getResource( IMG / logo.jpg)。。
使用该方法获取文件的URL并将其转换为字符串。然后,我必须将该字符串除以5,以摆脱此输出file:/ C:/Users/Stephen/git/ILLA/PoC/bin/img/logo.jpg
然而,当我将它导出为jar并运行它遇到麻烦。该URL现在显示为/ILLA.jar !/,我的图片只是空白。我有一个直觉,这是绊倒了我怎么解决这个问题?
干杯
参见这里:
String imgName =/resources/images/image.jpg;
InputStream in = getClass()。getResourceAsStream(imgName);
ImageIcon img = new ImageIcon(ImageIO.read(in));
请注意,您需要在档案中使用资源流。
I have a project with a image stored as a logo that I wish to use.
URL logoPath = new MainApplication().getClass().getClassLoader().getResource("img/logo.jpg");
Using that method I get the URL for the file and convert it to string. I then have to substring that by 5 to get rid of this output "file:/C:/Users/Stephen/git/ILLA/PoC/bin/img/logo.jpg"
However when I export this as a jar and run it I run into trouble. The URL now reads /ILLA.jar!/ and my image is just blank. I have a gut feeling that it's tripping me up so how do I fix this?
Cheers
See here: Create a file object from a resource path to an image in a jar file
String imgName = "/resources/images/image.jpg";
InputStream in = getClass().getResourceAsStream(imgName);
ImageIcon img = new ImageIcon(ImageIO.read(in));
Note it looks like you need to use a stream for a resource inside an archive.
这篇关于在Eclipse中导出一个JAR文件并引用一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!