Java get classpath — transfer

Classloader provides two methods to obtain resources from the loaded classpath: public URL getresource (string name); public InputStream getResourceAsStream(String name); Here, name is the classpath of the resource, which is relative to the position under the "/" root path. Getresource gets a URL object to locate the resource, and getresourceasstream gets the reference of the resource input stream to ensure that the program can extract data from the correct location. However, the two methods of classloader are not really used, but the getresource and getresourceasstream methods of class, because class objects can be obtained from your class (such as yourclass. Class or yourclass. GetClass ()), and classloader needs to call yourclass again Getclassloader () method, but according to the JDK document, these two methods of the class object are actually "delegate" to the classloader that loads it, so you only need to use these two methods of the class object. Therefore, call this. Getclass() getResourceAsStream(String name); Get the stream. In the static method, use classloader getSystemResourceAsStream(String name); 。 Here are some methods to get the classpath and the absolute path of the current class. You may need to use some of these methods to get the absolute path of the resources you need. 1.this. getClass(). Getresource ("") gets the URI directory of the current class file, excluding itself! For example: File: / D: / workspace / jbpmtest3 / bin / COM / test / 2. This. Getclass() Getresource ("/") gets the absolute URI path of the current classpath. For example: File: / D: / workspace / jbpmtest3 / bin / 3. This. Getclass() getClassLoader(). Getresource ("") also gets the absolute URI path of the current classpath. For example, file: / D: / workspace / jbpmtest3 / bin / 4. Classloader. Getsystemresource ("") also gets the absolute URI path of the current classpath. For example, file: / D: / workspace / jbpmtest3 / bin / 5. Thread. Currentthread() getContextClassLoader(). Getresource ("") also gets the absolute URI path of the current classpath. For example: File: / D: / workspace / jbpmtest3 / bin / 6. Servletactioncontext. Getservletcontext() Getrealpath ("/") in the web application, get the absolute path of the root directory of the web application. In this way, we only need to provide the path relative to the web application root directory to build the absolute path to locate the resource. For example: File: / D: / workspace / Metadata/. plugins/org. eclipse. wst. server. Core / tmp0 / wtpwebapps / webproject notes: 1 Try not to use relative to system getProperty("user.dir" )The relative path of the current user directory. This is a time bomb that could kill you at any time. 2. Try to use absolute path resources in the form of URI. It can be easily transformed into URI, URL and file objects. 3. Try to use the relative path of relative classpath. Do not use absolute paths. Using the public static URL getextendresource (string relativepath) method of the classloaderutil class above, we can locate resources at all locations using relative paths relative to the classpath. 4. Never use hard coded absolute paths. Because we can use the getresource ('') of the classloader class )Method to get the absolute path of the current classpath. If you must specify an absolute path, using a configuration file is much better than hard coding! Method to get the path outside classpath: URL base = this Getclass(). Getresource (""); / / get the location of this class first, such as / home / Popeye / testjava / build / classes / net / string path = new file (base. Getfile(), "... /... / + name). Getcanonicalpath(); / / get / home / Popeye / testjava / name. In addition, if you start the program from ant, this. Getclass() Getresource ("") is strange. It can be successfully debugged directly with Java command line.

Transferred from: http://hi.baidu.com/lzpsky/item/6ae30e5dec024c3494eb0526

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>