Java: how to obtain the path of the current jar package and how to read the resources in the jar package

Source: Internet
Author: User

Java: how to obtain the path of the current jar package and how to read the resources in the jar package

Output a record. dat file to the same directory of the jar package, but do not know how to locate the path of the jar package. Baidu's method is not very reliable, so record it here.

I. Use the class path

1 String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

Or

1 String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();

Because the program has been packaged into a jar package, the returned values of getPath () and getFile () are the same. All are in the form of/xxx. jar. If the path contains Unicode characters, you also need to transcode the path

path = java.net.URLDecoder.decode(path, "UTF-8");

Ii. Use JVM

String path = System.getProperty("java.class.path");

The system attribute of java Runtime is used to obtain the location of the jar file, which is also in the format of/xxx. jar.

 

In this way, the location of the jar package is obtained, but this is not enough. What we need is the directory of the jar package.

Use

1 int firstIndex = path.lastIndexOf(System.getProperty("path.separator")) + 1;2 int lastIndex = path.lastIndexOf(File.separator) + 1;3 path = path.substring(firstIndex, lastIndex);

To obtain the directory.

Path. separator in Windows; (semicolon); (colon) in Linux: (colon ). That is, the environment variables are often used to separate the two symbols of the PATH. For example, in Windows, we often set the environment variable PATH = xxxx \ xxx; xxx \ xxx; here we get this semicolon.

File. separator is/(slash) and \ (backslash), \ (backslash) in Windows, And/(slash) in Linux ).

How to load resources in a jar package.

1. For example, if I want to get a background image, in the source code, it is

/Src/UI/image/background.jpg

In the jar package, its path should be

/UI/image/background.jpg

The/at the beginning of the path indicates the root directory, that is, the absolute path. If there is no leftmost/, it indicates the relative path. Which method is used to view your needs? Here, the absolute path is used.

Used during loading

1 java.net.URL fileURL = this.getClass().getResource("/UI/image/background.jpg");2 javax.swing.Image backGround = new ImageIcon(fileURL).getImage();

You can obtain the image resource.

2. Sometimes, we need to load text or music resources, and the text exists as a stream object in Java, so we need to use

InputStream in = this.getClass().getResourceAsStream("/UI/image/background.txt");

Load the resource.

 

PS: note the differences between the two methods. The first is to first obtain the path of the file and then load the file resource. The second is to directly load the object.

 

3. Sometimes, we have some resource classes where the resource objects are modified by pulic static final. You can use this method to initialize them here.

For example, if I have an ImageSource class to load various image resources, you can use

1 public class ImageSource2 {3     static4     {5         URL fileURL = ImageSource.class.getResource(“/UI/image/background.jpg”);6         BACK_GROUND = new ImageIcon(fileURL).getImage();7     }8     public static final Image BACK_GROUND;9 }

Here, constructors cannot be used for initialization, because constructors are related to objects, while static variables are irrelevant to objects and only related to classes. In Java syntax, the static block in the class does not depend on the class object, so you can initialize the statc object. This cannot be used in static blocks, and ImageSource. class is used here.

 

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.