引用.jar文件中的图像 [英] Referencing an image in a .jar file

查看:85
本文介绍了引用.jar文件中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jar文件中引用简单图像时遇到了麻烦。

I am having real trouble referencing a simple image in a jar file.

我有一个包 au.com.mysite.pdf tha包含java文件
我有一个包 au.com.mysite.pdf.res 包含图像

I have a package au.com.mysite.pdf tha contains the java files I have a package au.com.mysite.pdf.res tha contains images

在我的java文件中,我引用了一个像这样的图像
getClass()。getResource(/ au / com / mysite / pdf / res / logo.png ).getPath()

In my java file I reference an image like this getClass().getResource("/au/com/mysite/pdf/res/logo.png").getPath()

但永远无法找到图像。在debutter中它看起来像
/C:/Documents%20and%20Settings/myname/workspace/gnupdf/bin/au/com/mysite/pdf/res/logo.png

but the image can never be found. In the debutter it looks like this /C:/Documents%20and%20Settings/myname/workspace/gnupdf/bin/au/com/mysite/pdf/res/logo.png

前导'/'是什么,而且分隔符不适用于Windows。

What is with the leading '/' and the separators are not correct for windows.

我检查了这条路径,它在MS Explorer中不起作用。

I checked this path and it does not work in MS Explorer.

好的,它是在某种程度上工作,除了图像路径不正确, /C:/Documents%20and%20Settings/myname/workspace/gnupdf/bin/au/com/mysite/pdf/res/logo.png 不是对文件的引用,前导斜杠和%20空格字符是什么?如何将其转换为文件URL而不是网址?

Ok, it is working to some extent except the image path is not correct, /C:/Documents%20and%20Settings/myname/workspace/gnupdf/bin/au/com/mysite/pdf/res/logo.png is not a reference to a file, what is with the leading slash and the %20 space characters? How do I convert this into a file URL instead of a web url?

推荐答案

如果图像在jar中,getResource将不会返回任何有用的内容。 getResourceAsStream可能就是你想要的。

If the image is in a jar, getResource is not going to return anything useful. getResourceAsStream is probably what you want.

这篇关于引用.jar文件中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆