Java obtains the code example of the current directory

Source: Internet
Author: User

Current Directory code

The code is as follows: Copy code

Import java. io .*;

Public class Test {
Public static void main (String [] args ){
System. out. println ("Current Working Dir:" + new File (""). getAbsolutePath ());
System. out. println ("Current Working Dir:" + System. getProperty ("user. dir "));
        }
}

Obtain the path of the current jar package

After testing, whether it is a Jar package or Tomcat deployment, the following three methods can be implemented.
 

The code is as follows: Copy code
Package test;
 
Public class MyPath {
Public static String getProjectPath (){
 
Java.net. URL url = MyPath. class. getProtectionDomain (). getCodeSource (). getLocation ();
String filePath = null;
Try {
FilePath = java.net. URLDecoder. decode (url. getPath (), "UTF-8 ");
} Catch (Exception e ){
E. printStackTrace ();
       }
If (filePath. endsWith (". jar "))
FilePath = filePath. substring (0, filePath. lastIndexOf ("/") + 1 );
Java. io. File file = new java. io. File (filePath );
FilePath = file. getAbsolutePath ();
Return filePath;
}
 
Public static String getRealPath (){
String realPath = MyPath. class. getClassLoader (). getResource (""). getFile ();
Java. io. File file = new java. io. File (realPath );
RealPath = file. getAbsolutePath ();
Try {
RealPath = java.net. URLDecoder. decode (realPath, "UTF-8 ");
} Catch (Exception e ){
E. printStackTrace ();
       }
 
Return realPath;
    }
 
Public static String getAppPath (Class <?> Cls ){
// Check whether User-passed parameters are null
If (cls = null)
Throw new java. lang. IllegalArgumentException ("The parameter cannot be blank! ");
 
ClassLoader loader = cls. getClassLoader ();
// Obtain the full name of the class, including the package name
String clsName = cls. getName ();
// Check whether the SDK is a Java Base Class Library to prevent users from passing in the built-in JDK class library.
If (clsName. startsWith ("java.") | clsName. startsWith ("javax .")){
Throw new java. lang. IllegalArgumentException ("do not transmit the system class! ");
        }
// Change the full name of the class file to the path format
String clsPath = clsName. replace (".", "/") + ". class ";
 
// Call the getResource method of ClassLoader to pass in the class file name containing path information
Java.net. URL url = loader. getResource (clsPath );
// Obtain the path information from the URL object
String realPath = url. getPath ();
// Remove the protocol name "file:" from the path information :"
Int pos = realPath. indexOf ("file :");
If (pos>-1 ){
RealPath = realPath. substring (pos + 5 );
        }
// Remove the last part of the path information that contains the class file information to obtain the path of the class.
Pos = realPath. indexOf (clsPath );
RealPath = realPath. substring (0, pos-1 );
// If the class file is packaged into a JAR file, remove the corresponding JAR and other packaging file names.
If (realPath. endsWith ("! ")){
RealPath = realPath. substring (0, realPath. lastIndexOf ("/"));
        }
Java. io. File file = new java. io. File (realPath );
RealPath = file. getAbsolutePath ();
 
Try {
RealPath = java.net. URLDecoder. decode (realPath, "UTF-8 ");
} Catch (Exception e ){
Throw new RuntimeException (e );
        }
Return realPath;
} // GetAppPath definition ends
}
 


Using the Jar package, the running results in Tomcat are as follows:

ProjectPath: D:/J2EE/Tomcat 6.0/webapps/MyService1WebP/WEB-INF/lib
RealPath: D:/J2EE/Tomcat 6.0/webapps/MyService1WebP/WEB-INF/classes
Apppath: D:/J2EE/Tomcat 6.0/webapps/MyService1WebP/WEB-INF/classes

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.