Java jar package image Path Problems

Source: Internet
Author: User

Every time you package a jar package, you will always encounter problems where images cannot be displayed. So let's take a moment to summarize,

Two methods are summarized:

1. Put the jar package and image folder in a folder, and the relative path is in this folder. For example, call the image in the image folder and directly use new imageicon ("image/logo.jpg. However, this method is not good. After all, we have packaged the program and put the picture out of the package.


2. This method is definitely to put the image folder into the jar package. Note that the image folder you want to package must be placed in the SRC folder of the project (or the folder below SRC); otherwise, you cannot enter the jar package. Then

1 ). the image folder can be placed in the same folder as the Java class file. In this way, you can directly use the relative path in the jar when packaging the file. The method is: this. getclass (). getresource ("image/logo.jpg ");

2 ). if the image folder is not in the same folder as the Java class file, you can use the absolute path in the jar. the root directory of the jar package is/(SRC can be viewed as the root directory ), if you put the image folder in the SRC folder, you can use the following method to call the image: This. getclass (). getresource ("/image/logo.jpg"); note that "/" is added here, so that the absolute path in the jar package is used.

Method reference


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.