Using Resources from the External JAR File
- Skill Level Intermediate
- Product JavaFX
- Key Features Graphics API Animation
- Last Updated July 2009
Loading images from the current JAR file or from a specific URL is common practice when using JavaFX Script. Refer to the Image class documentation for more details.
But what if you need to use an image from the existing external JAR file? For example, you want to reuse a standard icon. One possibility is to duplicate the image file and add it to another JAR. However, this approach has some disadvantages. The size of the application will increase and, consequently, the launch time will increase.
In addition, the image update process will also become more complicated. If you will ever need to change the icon image, you will need to perform updates in several JARs archives.
Fortunately, there is a better and yet simpler solution. JavaFX Script enables developers to employ all Java capabilities to link resources, in particular, to access a file from the specified JAR file. Use a URL of the following type - jar:{url}!/{file}. Here is an example:
def url = "http://javafx.com/samples/PathAnimation/webstart/PathAnimation.jar";
def file = "pathanimation/resources/car.png";
def image = Image {
url: "jar:{url}!/{file}"
}
Find more details on this solution in Sergey Malenkov's blog.
Refer to Getting Started With JavaFX Production Suite and API documentation for more information about loading JavaFX Graphics from a FXD file or FXZ archive.
We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of repliesyour information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.
Sergey Malenkov
Software Engineer, Sun Microsystems