在Eclipse中导出一个JAR文件并引用一个文件

在Eclipse中导出一个JAR文件并引用一个文件

本文介绍了在Eclipse中导出一个JAR文件并引用一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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文件并引用一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:42