There are two types:
First, the resource file is a general suffix file.
Type 2: resource files are image files
[No1] First
You can use this line of code to obtain the path to the root directory of the class.
String Path = thread. currentthread (). getcontextclassloader (). getresource (""). getpath ();
Example: My development software myeclipse 6.5
Assume that the project folder is as follows:
Files --- bin -- core (generate class package)
| -- Main. Class (generated class file)
|
| -- Resource (generate resource folder)
| -- A. bat
| --B.png
| --- SRC -- core (source package)
| -- Main. Java (source code)
|
| -- Resource (source resource folder)
| -- A. bat
| --B.png
// Source code main. Java
// ================================================ ======================================
Package core;
Import java. Io. file;
Public class main {
Public static void main (string [] ARGs ){
Try {
String Path = thread. currentthread (). getcontextclassloader (). getresource (""). getpath (); // Add
File AF = new file (path + "/resource ");
If (! Af. exists () system. Out. println ("nullexist ");;
String [] files = AF. List ();
If (files. Length = 0) system. Out. println ("nulllength ");;
For (INT I = 0; I <files. length; I ++ ){
If (files [I]! = NULL) system. Out. println (files [I]);
Else system. Out. println ("null ");
}
}
Catch (exception e ){
System. Out. println ("hugeerror ");
}
}
}
// ================================================ ======================================
Running result:
A. bat
B .png
That's right.
[NO2] type 2
We recommend that you use the (2) method below, because (1) problems that may not be found after the jar (I tried this before)
Code saved here
(1) You package all the resources. jar, your class is in a package: Package core; all your image resources are in the images folder, and the images folder is also in the core package. In this case, the final path is as follows:
EPM --- bin -- core (generate class package)
| -- Main. Class (generated class file)
|
| -- Images (generate resource folder)
| -- System. bat
| --Background.png
| --- SRC -- core (source package)
| -- Main. Java (source code)
|
| -- Images (source resource folder)
| -- System. bat
| --Background.png
You can access it through the relative path:
Java.net. url imurl = getclass (). getresource ("images/background.png ");
Imageicon im = new imageicon (imurl );
(2) In another case, if you have many classes and the package structure is very complicated, you should put the image on the outermost layer, allow all classes to access the image through an absolute path
EPM --- bin -- core (generate class package)
| -- Main. Class (generated class file)
|
| -- Images (generate resource folder)
| -- System. bat
| --Background.png
| --- SRC -- core (source package)
| -- Main. Java (source code)
|
| -- Images (source resource folder)
| -- System. bat
| --Background.png
Java.net. url imurl = getclass (). getresource ("/images/background.png ");
Imageicon im = new imageicon (imgurl );
The difference is very subtle. Only a backslash "/" is added before "Images". This backslash indicates the root directory, and the relative path is indicated without a backslash.
These are all careful questions ......