Java or jar package to obtain the path to read the resource file

Source: Internet
Author: User

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 ...... 

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.