Java gets the path of the current class

Source: Internet
Author: User

1. This insecure approach:

1. New file (PATH). The path of this method depends on the starting position of the Java command to be called,

Catalina. BAT in Tomcat/bin calls Java, so the relative start position in Tomcat is tomcat/bin, but the start position in eclipse is the Eclipse project path at eclipse startup.

2. Class. Class. getclassloader (). getresource (""). getpath ()

If this method is used, the decision is handed over to the Class Loader. For example, Tomcat class loading is a non-commissioned mechanism, while WebLogic class loading is delegated. In most cases, it is safe.

3. Class. Class. getresource ("")

This is a good method, but the limitation is that if the class file is in jar, you need to input the folder together when creating the jar package; otherwise, null is returned, the jar file is actually a zip file. In the ZIP file, the file is a file, and the folder is a folder, not associated, many open-source jar packages do not include directories and only file classes. Although you can reach the directory hierarchy, you can call classes. class. getresource ("") returns NULL. because the directory structure of the file is different from the folder itself.

 

Obtain the path of a class: Obtain the location where the class itself stores files in the system, and then locate the classpath according to the package level:

Package Com. wbtask;

ImportJava. Io. file;
ImportJava.net. url;

Public   Class Urlutil {
/**
* Obtain the file of the current class.
* @ Param Clazz
* @ Return
*/
Public   Static File getclassfile (class clazz ){
URL path = Clazz. getresource (clazz. getname (). substring (
Clazz. getname (). lastindexof ( " . " ) + 1 ) + " . Classs " );
If (Path =   Null ){
String name = Clazz. getname (). replaceall ( " [.] " , " / " );
Path = Clazz. getresource ( " / " + Name + " . Class " );
}
Return   New File (path. GetFile ());
}
/**
* Obtain the path of the current class.
* @ Param Clazz
* @ Return
*/
Public   Static String getclassfilepath (class clazz ){
Try {
Return Java.net. urldecoder. Decode (getclassfile (clazz). getabsolutepath (), " UTF-8 " );
} Catch (Exception e ){
// Todo: handle exception
E. printstacktrace ();
Return   "" ;
}
}

/**
* Obtain the classpath directory of the current class, such as the classes path under tomcat.
* @ Param Clazz
* @ Return
*/
Public   Static File getclasspathfile (class clazz ){
File = Getclassfile (clazz );
For ( Int I = 0 , Count = Clazz. getname (). Split ( " [.] " ). Length; I < Count; I ++ )
File = File. getparentfile ();
If (File. getname (). touppercase (). endswith ( " . Jar! " )){
File = File. getparentfile ();
}
Return File;
}
/**
* Obtain the classpath path of the current class.
* @ Param Clazz
* @ Return
*/
Public   Static String getclasspath (class clazz ){
Try {
Return Java.net. urldecoder. Decode (getclasspathfile (clazz). getabsolutepath (), " UTF-8 " );
} Catch (Exception e ){
// Todo: handle exception
E. printstacktrace ();
Return   "" ;
}
}

Public   Static   Void Main (string [] ARGs ){
System. Out. println (getclassfilepath (urlutil. Class ));
System. Out. println (getclasspath (urlutil. Class ));
}

}

Result:

E: \ workspaces \ myproject \ pjoname \ target \ Classes \ com \ wbtask \ urlutil. Class
E: \ workspaces \ myproject \ pjoname \ target \ Classes
Related Article

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.