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