简体   繁体   English

将Java文件编译为jar文件后,为什么图像路径不再起作用?

[英]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. 图像文件是否嵌入。

Embedded Resource 嵌入式资源

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) 因为通常比较难破解(恕我直言)

External Resource 外部资源

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.

相关问题 当我从jar运行我的应用程序时,为什么新的File(path)不起作用? - Why does new File(path) doesn't work when I run my application from jar? 在Java中,为什么class.getResource(“ / path / to / res”); 将文件复制到系统时,无法在可运行的jar中工作? - In Java, why does class.getResource(“/path/to/res”); not work in the runnable jar when copying files to the system? 编译jar,netbeans后,无法加载src中的图像文件 - Image files in my src not loading after i compile the jar, netbeans 当它需要jar中的类时,为什么不能在cygwin上编译我的Java文件? - Why can't I compile my java file on cygwin, when it needs classes from a jar? 如何将Java准确地编译为JAR文件? - How exactly do I compile my Java into a JAR file? 我将图像文件保存在Java项目中以创建.jar的位置 - Where I keep my image files in java project to make .jar 当我将项目导出到JAR文件时,SourceDataLine不起作用 - The SourceDataLine does not work when I export my project into JAR file 当我将Java导出为可运行的jar文件时,不起作用 - When i export my java as runnable jar file, do not work 编译代码时,JAVA不能在Netbeans Project和JAR文件上使用相同的资源 - JAVA does not work with the same resources on Netbeans Project and JAR file when the compile the code 为什么getResourceAsStream()在JAR文件中不起作用? - Why does getResourceAsStream() not work in JAR files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM