The path problem after Java packaging

Source: Internet
Author: User
Solving the path problem of resource files when Java applications are packaged

Use NetBeans to do an application, which has been applied to some pictures, there are several sound files, debugging run no problem, after packaging to build the jar, run the jar file, these pictures, sound does not come out. This question puzzled me for a long time.

Look for examples of books, search network articles, their own tried a variety of methods, the problem gradually clear. Today finally found the root cause of the problem, there is a sigh of "so". The painful experience will not be said. Write down the information you have found and your own ideas, which is a summary of yourself, and may also be helpful to others. Especially the people who are trying to do Java programming in the first place. 1. First of all, the access path problem for the resource in the application

Ways to get file paths:

(1) String path = System.getproperty ("User.dir");

(2) Properties Properties = System.getproperties ();
System.out.println (Properties.getproperty ("User.dir"));

(3) File File=new file (".");
System.out.println (File.getabsolutepath ());

(4) This.getclass (). GetResource ("/");

(5) ClassLoader cl = This.getclass (). getClassLoader ();
Icon Saveicon = new ImageIcon (Cl.getresource ("images/save.gif"));

Generally there are so many, there are some other methods. Say no more. 2. Tell me why it's a problem when you pack it up

This problem actually involves one of the fundamental principles of the Java language:

For the win operating system, the capitalization of its folders, filenames, and extensions is indistinguishable. But we are packaged in the jar file to read the resource is case-sensitive, so add resources, reference resources must pay attention to the folder name, file name, extension of the case, it is recommended to use a unified approach to programming, either uppercase, or lowercase, preferably all lowercase.

Let's take an example. I use a picture as a button icon in my program:

Btn.seticon (New ImageIcon (GetClass (). GetResource ("resources/pic01.jpg"));

Here, use the GetClass (). GetResource ("") to get the relative path. The picture pic01.jpg is located in the resources in the project file.

This has not solved the problem, because my picture's extension is uppercase JPG, so has been debugging run through, packaging will not show the picture.


Go from: Java Packaged path issues

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.