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 useSystem. 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 ..