Get classpath in Java

Source: Internet
Author: User

ClassloaderTwo methods are provided to obtain resources from the loaded class path:

Public URLGetresource(String
Name );
Public inputstreamGetresourceasstream(String
Name );

Here name is the class path of the resource, which is relative to the location under the "/" root path. Getresource obtains a URL object to locate the resource, while getresourceasstream obtains the reference of the resource input stream to ensure that the program can extract data from the correct position.
But what we actually use is not the classloader's two methods, but the getresource and getresourceasstream methods of the class, because the class object can be obtained from your class (such as yourclass. class or yourclass. getclass (), while classloader needs to call yourclass again. the getclassloader () method, but according to the JDK documentation, the two methods of the class object are actually "delegate" (delegate) to the classloader that loads it, so you only need to use
The two methods of the class object can be used.

Therefore, directly call this. getclass (). getresourceasstream (string
Name); get the stream. In the static method, use classloader. getsystemresourceasstream (string
Name );.

The following are some methods to obtain the absolute path of classpath and the current class. You may need to use some of these methods to obtain the absolute path of the resources you need.

1. This. getclass (). getresource ("")
The URI Directory of the current class file is obtained. Not yourself!
For example, file:/D:/workspace/jbpmtest3/bin/COM/test/

2.This. getclass (). getresource ("/")
Obtain the absolute URI path of the current classpath.
For example, file:/D:/workspace/jbpmtest3/bin/

3. This. getclass ().Getclassloader (). getresource ("") 
The obtained absolute URI path of the current classpath.
For example, file:/D:/workspace/jbpmtest3/bin/

4.Classloader. getsystemresource ("")
The obtained absolute URI path of the current classpath.
For example, file:/D:/workspace/jbpmtest3/bin/

5.Thread. currentthread (). getcontextclassloader(). Getresource ("") 
The obtained absolute URI path of the current classpath.
For example, file:/D:/workspace/jbpmtest3/bin/

6.Servletactioncontext. getservletcontext (). getrealpath ("/")
In the web application, obtain the absolute path of the root directory of the Web application. In this way, we only need to provide the path relative to the root directory of the Web application program to build an absolute path to locate the resource.
For example, file:/D:/workspace/. Metadata/. plugins/org. Eclipse. WST. server. Core/tmp0/wtpwebapps/webproject


Note:

1. Try not to use the relative path relative to the current user directory of system. getproperty ("user. dir. This is a timed bomb, and it may take your life at any time.

2. Try to use absolute path resources in Uri format. It can easily be converted to Uri, URL, and file objects.

3. Try to use the relative path of the classpath. Do not use absolute paths. You can use the public static URL getextendresource (string relativepath) method of the classloaderutil class above to locate resources in all locations using the relative path relative to classpath.

4. Do not use hard-coded absolute paths. Because we can use the getresource ("") method of the classloader class to obtain the absolute path of the current classpath. If you must specify an absolute path, it is much better to use the configuration file than hard encoding!

To obtain a path other than classpath:
URL base = This. getclass (). getresource (""); // first obtain the location of the class, such as/home/Popeye/testjava/build/classes/NET/
String Path = new file (base. GetFile (),"...... /...... /...... /"+ Name). GetCanonicalPath (); // You Can Get/home/Popeye/testjava/name.

In addition, if you start the program from ant, this. getclass (). getresource ("") is more strange, you can directly use the Java command line for debugging.

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.