Java absolute Path/relative path summary

Source: Internet
Author: User
Tags tomcat server

Note: Relative paths to the current user directory
Is the path returned relative to System.getproperty ("User.dir").
For a generic project, this is the root path of the project. For Java EE servers, this could be a path to the server. This does not have a uniform norm!
Therefore, never use a relative path to the current user directory. However:
By default, the classes in the Java.io package always parse relative path names based on the current user directory. This directory is specified by the system property User.dir, which is usually the calling directory for the Java virtual machine.
This means that when using classes in the Java.io package, it is best not to use relative paths. Otherwise, although in the J2SE application may be normal, but to the Java EE program, there will be a problem! And this path, in different servers are different!

a,  relative path best practices
recommended relative paths to current classpath
Therefore, when we use relative paths, Relative paths to the current classpath should be used. The
ClassLoader class GetResource (string name), getResourceAsStream (string name), uses a relative path to the classpath of the current project to find the resource. The same is true of the Getbundle (String path) of the ResourceBundle class that is commonly used by the
Read property file.

Here are some ways to get the absolute path of classpath and the current class. You may need to use some of these methods to get the absolute path of the resources you need.

1.filetest.class.getresource ("")
The URI directory of the current class Filetest.class file is obtained. Don't include yourself!

such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/com/test/

2.filetest.class.getresource ("/")
Gets the absolute URI path of the current classpath.

such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/

3.thread.currentthread (). Getcontextclassloader (). GetResource ("")
The resulting absolute URI path is also the current classpath.

such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/

4.filetest.class.getclassloader (). GetResource ("")
The resulting absolute URI path is also the current classpath.

such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/

5.classloader.getsystemresource ("")
The resulting absolute URI path is also the current classpath.

such as: file:/d:/java/eclipse32/workspace/jbpmtest3/bin/

I recommend using Thread.CurrentThread (). Getcontextclassloader (). GetResource ("") to get the URI representation of the absolute path of the current classpath.

Two, read and write in 1.servlet. If using struts or a servlet can be configured directly in the initialization parameters, the call is based on the servlet's Getrealpath ("/") to get the real path, and then the string file = This.servlet.getInitParameter ("abc"); Gets the relative path of the relative Web-inf.
Example:
InputStream input =thread.currentthread (). Getcontextclassloader (). getResourceAsStream ("Abc.properties"  );  
Properties prop = new properties ();
Prop.load (input);
Input.close ();  

Prop.setproperty ("abc", "Test");
Prop.store (new FileOutputStream (path), "–test–"),  
Out.close ();  

2. Directly in the JSP operation, Get an actionable absolute address from a JSP built-in object.  

//JSP page
String path = Pagecontext.getservletcontext (). Getrealpath ("/");
String Realpath = path+ "/web-inf/classes/abc.properties";

//java program
InputStream in = GetClass (). getClassLoader (). getResourceAsStream ("abc.properties");//  Abc.properties placed in the webroot/web-inf/classes/directory
Prop.load (in);
In.close ();

  Reprinted from: 51144588/

An additional article about absolute paths. Important article relative path:38753047

relative and absolute paths to the file class    

The file class is the class used to construct files or folders, and in its constructor, it is required to pass in a string parameter that indicates the path where the file is located. The absolute path has been used previously as a parameter, but a relative path can also be used here. Using absolute paths Needless to say, it's easy to locate a file, Then the relative path is used

How does the JVM locate files?

According to the JDK Doc, the absolute pathname is the full pathname, and no additional information is required to locate the file that it represents. Instead, the relative pathname must be interpreted using information from other pathname names. By default, the classes in the Java.io package always parse relative path names based on the current user directory. This directory is specified by the system property User.dir, which is usually the calling directory for the Java virtual machine. "

Relative path as the name implies, relative to a certain path, we have to figure out what the path is relative to. The path described in the JDK above is "Current user directory" and "Java Virtual machine Call directory". More clearly, the path is where we call the JVM. For example:

Suppose there is a Java source file Example.java in the D packing directory, the file does not contain package information. We go to the Command Line window, then use "D:" command to switch to the D-Packing directory, and then use "Javac Example.java" to compile this file, compile error-free , the "Example.class" file is automatically generated in the D-packing directory. We are invoking "Java Example" to run the program. At this point we have started a JVM, which was launched under the D-Packing directory, So the relative path of the file class in the program loaded by this JVM is relative to this path, that is, the D packing directory: D:\. Also the "Current user directory" is also d:\. In System.getproperty ("User.dir"); system variable "User.dir" This value is also stored.

We can do a few more experiments, move "example.class" to different paths, and under those paths, execute the "Java Example" command to start the JVM, we will find this "Current user directory" is constantly changing, Its path is always the same as where we started the JVM.

Knowing this, we can use relative paths to create files, for example:

File File = new file ("A.txt");

File.createnewfile ();

Assuming that the JVM was started under "D:\", then A.txt would be generated in D:\a.txt;

In addition, this parameter can also use some common path representations, such as "." or ". \" represents the current directory, which is the JVM boot path. So the following code can get the full path of the current directory:

File F = new file (".");

String Absolutepath = F.getabsolutepath ();

System.out.println (Absolutepath);//d:\

Finally, the situation in eclipse:

The startup JVM in Eclipse is started on the project root path. For example, there is a project named blog, the full path is: D:\work\IDE\workspace \blog. So this path is the JVM's boot path. So the above code if run in eclipse , the output is "D:\work\IDE\workspace\blog."

The situation in Tomcat.

If you are running a web app in Tomcat, at this point, if we use the following code in a class:

File F = new file (".");

String Absolutepath = F.getabsolutepath ();

System.out.println (Absolutepath);

Then the output will be the bin directory under Tomcat. My machine is "D:\work\server\jakarta-tomcat-5.0.28\bin\.", so you can see that the Tomcat server is starting the JVM in the bin directory In fact, the JVM is started in the "Catalina.bat" file in the bin directory.

Attached: Java's System.getproperty () method can get the value: 3987198

Java absolute Path/relative path summary

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.