Current path of the program

Source: Internet
Author: User

Move from my Netease blog.

In general, open a file in the current path. That's all.

File f = new File ("1.txt ");

It runs normally on eclipse. The path is the directory of the current project.
But after packaging. Run the jar package. Although the txt file is still included in the jar package. But it cannot be found.
Later I found out. The execution path ran to C: \ windows \ win32.
Try to use

System. getProperty ("user. dir"): // the path of the current project.

To obtain the current path. It is still the same. The path on eclipse is correct. However, after packaging, the path for executing jar runs to C: \ windows \ win32.
To obtain the compiled class path, use

System. getProperty ("java. class. path"); // the top-level directory and jar package path of the compiled class path are obtained.

However, the path must be processed. That is, to obtain the upper-level directory, and I have not found any function with a processing path in java. No way, only self-reliance.

String temppath = System. getProperty ("java. class. path "); String path = temppath. substring (0, temppath. lastIndexOf ("\") + "\ 1.txt ";

Path processed above. You can get the current path of the jar package. In eclipse and directly execute the jar package to run normally ..

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.