Absolute Path of Project's folder in Java

前端 未结 3 1236
醉酒成梦
醉酒成梦 2020-12-09 23:34

Lots of confusion in this topic. Several Questions have been asked. Things still seem unclear. ClassLoader, Absolute File Paths etc etc

Suppose I have a project dire

3条回答
  •  悲&欢浪女
    2020-12-10 00:16

    @Mark is correct. That is by far the simplest and most robust approach.

    However, if you really have to have a File, then your best bet is to try the following:

    • turn the contents of the System property "java.class.path" into a list of pathnames,
    • identify the JAR pathname in the list based on its filename,
    • figure out what "../.." is relative to the JAR pathname to give you the "project" directory, and
    • build your target path relative to the project directory.

    Another alternative is to embed the project directory name in a wrapper script and set it as a system property using a -D option. It is also possible to have a wrapper script figure out its own absolute pathname; e.g. using whence.

提交回复
热议问题