Java method for obtaining the current path in different environments-This. getclass (). getresource ("")

Source: Internet
Author: User
1. In Servlet/filter and other servlet web environments, as long as the javax. servlet. servletcontext type is obtained, the path can be obtained through getrealpath. The top-level directory in the relative path can be obtained through the parameter.
Request. getsession (). getservletcontext (). getrealpath ("/");

2. jsp custom tag javax. servlet. jsp. tagext. tagsupport
(Javax. servlet. servletcontext) pagecontext). getrealpath ("");

3. Common Java class object files:
This. getclass (). getresource ("??? ");
If the class file is in the top-level directory (package) and Is a blank string ("), and if this method is executed in the jar file, null is returned. Directories (packages) under the top-level directory (packages) will return URLs containing the protocol. "/" Is used to separate folders (packages) at different layers.

Project location: D:/beyondtrack -- reference/jbpmwebdesigner

Javacode location: D:/beyondtrack -- reference/jbpmwebdesigner/build/web/classes/NET/Java/dev/beyondtrack/web/workflow/DESIGNER/japplet

File Location: D:/beyondtrack -- reference/jbpmwebdesigner/src/Java/NET/Java/dev/beyondtrack/web/workflow/DESIGNER/icon

/*
* To change this template, choose tools | templates
* And open the template in the editor.
*/
Package net. java. Dev. beyondtrack. Web. workflow. Designer. japplet;

Import javax. Swing. imageicon;

/**
*
* @ Author pub32
*/
Public class newmain {

Public newmain (){
System. Out. println (this. getclass (). getresource (""));
System. Out. println (this. getclass (). getresource ("."));
System. Out. println (this. getclass (). getresource (".."));
System. Out. println (this. getclass (). getresource ("/"));
System. Out. println (this. getclass (). getresource ("point.png "));
System. Out. println (this. getclass (). getresource ("/point.png "));
System. Out. println (this. getclass (). getresource ("./point.png "));
System. Out. println (this. getclass (). getresource ("../point.png "));
System. Out. println (this. getclass (). getresource ("icon/point.png "));
System. Out. println (this. getclass (). getresource ("/icon/point.png "));
System. Out. println (this. getclass (). getresource ("./icon/point.png "));
System. Out. println (this. getclass (). getresource ("../icon/point.png "));
}

Public static void main (string [] ARGs ){
System. Out. println ("====== test. Class ==== ");
Newmain test = new newmain ();
}
}

====== Test. Class ======
File:/D:/beyondtrack -- reference/jbpmwebdesigner/build/web/WEB-INF/classes/NET/Java/dev/beyondtrack/web/workflow/DESIGNER/japplet/
File:/D:/beyondtrack -- reference/jbpmwebdesigner/build/web/WEB-INF/classes/NET/Java/dev/beyondtrack/web/workflow/DESIGNER/japplet/
File:/D:/beyondtrack -- reference/jbpmwebdesigner/build/web/WEB-INF/classes/NET/Java/dev/beyondtrack/web/workflow/DESIGNER/
File:/D:/beyondtrack -- reference/jbpmwebdesigner/build/web/WEB-INF/classes/
Null
Null
Null
Null
Null
Null
Null
File:/D:/beyondtrack -- reference/jbpmwebdesigner/build/web/WEB-INF/classes/NET/Java/dev/beyondtrack/web/workflow/DESIGNER/icon/point.png
Build successful (total time: 0 seconds)

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.