Relative path to Java file

Source: Internet
Author: User
Several times, in Java in the relative path to open the file, are reported to find the file error, changed to absolute path after normal.


today to examine how the relative path should be written.





Test Code:


public class Testfilepath {


? public static void Main (string[] args) {


??? Testfilepath testFilePath1 = new Testfilepath ();


??? File File = new file ("Test.txt");


??? System.out.println (File.getabsolutepath ());


? }


}





to the command line to execute the class file with a Java command, executed under different paths, and the results are not the same.


is as follows:


Execution Method 1:


F:\2004\projects\jbproject\myserver\classes>java Test_bak. Testfilepath


F:\2004\projects\jbproject\myServer\classes\test.txt


Execution Method 2:


F:\2004\projects\jbproject\myserver>java-classpath classes Test_bak. Testfilepath


F:\2004\projects\jbproject\myServer\test.txt


Execution Method 3:


F:\2004\projects\jbproject>java-classpath myserver/classes Test_bak. Testfilepath


F:\2004\projects\jbproject\test.txt





is visible, relative to the starting point of the directory, not the path where the class file resides, but the location where the Java interpreter resides.





However, JBuilder executes under the jbuilder default command, H:\JBuilder9\jdk1.4\bin\javaw-classpath ... test_bak. Testfilepath


execution results are F:\2004\projects\jbproject\myServer\test.txt


So, if the Java application Operation file is developed under JBuilder, the starting point of the relative directory is generally the Classes\ directory, which is the application directory, the written program,


execution is performed with-classpath classes, and the same result can be obtained with development.





?














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.