How to locate the Path of the current project directory in Java (IDE)?

前端 未结 9 1383
梦如初夏
梦如初夏 2020-11-28 21:46

I am trying to locate the path of the current running/debugged project programmatically in Java, I looked in Google and what I found was System.getProperty(\"user.id\"

9条回答
  •  情深已故
    2020-11-28 22:13

    Two ways

    System.getProperty("user.dir");
    

    or this

    File currentDirFile = new File(".");
    String helper = currentDirFile.getAbsolutePath();
    String currentDir = helper.substring(0, helper.length() - currentDirFile.getCanonicalPath().length());//this line may need a try-catch block
    

    The idea is to get the current folder with ".", and then fetch the absolute position to it and remove the filename from it, so from something like

    /home/shark/eclipse/workspace/project/src/com/package/name/bin/Class.class
    

    when you remove Class.class you'd get

    /home/shark/eclipse/workspace/project/src/com/package/name/bin/
    

    which is kinda what you want.

提交回复
热议问题