[英]Why does an image path no longer work once I compile my java files into a jar file?
I have a relatively basic java program which uses a system tray icon. 我有一个使用系统任务栏图标的相对基本的Java程序。 The path I was using while writing the code is as follows "../images/logo.png"
. 我在编写代码时使用的路径如下"../images/logo.png"
。 However, when I compile it into a jar file, the image does not show up in the system tray. 但是,当我将其编译为jar文件时,该图像不会显示在系统托盘中。 Instead, if I change the path to "./images/logo.png"
, then the image shows up in the system tray when it's in the jar file form, but not while I'm testing. 相反,如果我将路径更改为"./images/logo.png"
,则该图像以jar文件形式出现在系统托盘中,而不是在测试时出现。
It's not a major issue. 这不是主要问题。 However, I am curious to know why this inconsistency occurs. 但是,我很好奇为什么会出现这种不一致的情况。
When you package your program into a .jar file, your build is most likely copying the image into the same directory as the .jar file. 将程序打包为.jar文件时,您的构建很可能会将映像复制到.jar文件所在的目录中。 However, when debugging in your ide, your image file lies one directory below. 但是,在ide中调试时,映像文件位于下面的一个目录中。
Another possibility is that you are simply setting your Working Directly differently in the two scenarios. 另一种可能性是,您只是在两种情况下将“直接工作”设置为不同。
Incidentally, you might be interested in embedding the image in your jar file, see: https://stackoverflow.com/a/1096491/24954 顺便说一句,您可能有兴趣将图像嵌入到jar文件中,请参阅: https : //stackoverflow.com/a/1096491/24954
This answer depends on two things. 这个答案取决于两件事。 If the image file is embedded or not. 图像文件是否嵌入。
Once you have Jar'ed your application and the images are emebbed inside the application, normal file access methods will no longer work. 一旦对应用程序进行Jar'ed操作并且图像被嵌入应用程序内部,常规的文件访问方法将不再起作用。
Trying to do something like... 尝试做类似...
new ImageIcon("../images/logo.png");
or 要么
new File("../images/logo.png");
Won't work. 不行 This is because the resource is no longer a file within the context of the file system (it's actually a Zip entry in the Jar). 这是因为资源不再是文件系统上下文中的文件(实际上是Jar中的一个Zip条目)。
Instead, you need to use Class#getResource
which will return a URL
to the embedded resource. 相反,您需要使用Class#getResource
,它将返回URL
到嵌入式资源。
new ImageIcon(getClass().getResource("../images/logo.png"));
Will work better. 会更好地工作。 In general though, it is recommended to use an absolute path to the resources new ImageIcon(getClass().getResource("/images/logo.png"));
不过,一般而言,建议对资源使用绝对路径new ImageIcon(getClass().getResource("/images/logo.png"));
as it's generally more difficult to break (IMHO) 因为通常比较难破解(恕我直言)
The path to the image is relative to the execution point of the application. 图像的路径相对于应用程序的执行点。
In development, you may have had to move up a directory (out of the src
folder presumably) to find the image resource. 在开发中,您可能必须向上移动目录(可能是src
文件夹中的目录)才能找到图像资源。 This will mean that you will need to store you Jar file in a folder that would require it step up one level before it could find the image resource. 这意味着您将需要将Jar文件存储在一个文件夹中,这需要将它逐步升级才能找到图像资源。
If you can, it's generally better to embedded the resource within the Jar where possible. 如果可以的话,通常最好将资源嵌入Jar中。 It makes it easier to deploy as you reduce the number of files you need to package and makes it (a little) harder for the user to mess with it ;) 当您减少需要打包的文件数时,它使部署变得更容易,并使用户更难以(有点)弄乱它;)
无法识别图像的路径,因此可以使用此代码
jLable1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Client/images/images/button_ok.png")));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.