5 ways to get class load path and project root path in Java analysis _java

Source: Internet
Author: User
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"));

}
}

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.