Copy Code code as follows:
Package my;
Import Java.io.File;
Import java.io.IOException;
Import Java.net.URL;
public class Myurldemo {
public static void Main (string[] args) {
Myurldemo Mudemo = new Myurldemo ();
try {
Mudemo.showurl ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public void Showurl () throws IOException {
First: Get the root path loaded by the class D:\git\daotie\daotie\target\classes
File F = new file (This.getclass (). GetResource ("/"). GetPath ());
System.out.println (f);
Gets the project path of the current class; If you do not add "/" to get the current class's Loading directory D:\git\daotie\daotie\target\classes\my
File F2 = new file (This.getclass (). GetResource (""). GetPath ());
SYSTEM.OUT.PRINTLN (F2);
Second: Get the project path D:\git\daotie\daotie
File directory = new file ("");//parameter is empty
String coursefile = Directory.getcanonicalpath ();
System.out.println (Coursefile);
The third type: file:/d:/git/daotie/daotie/target/classes/
URL Xmlpath = This.getclass (). getClassLoader (). GetResource ("");
System.out.println (Xmlpath);
The fourth kind: D:\git\daotie\daotie
System.out.println (System.getproperty ("User.dir"));
/*
* Results: C:\Documents and Settings\administrator\workspace\projectname
* Get Current engineering path
*/
Fifth: Get all classpath including jar package path
System.out.println (System.getproperty ("Java.class.path"));
}
}