零零乙 2011-02-02 05:24 采纳率: 33.3%
浏览 359

在 Java 中获得当前的工作目录

I want to access my current working directory using

 String current = new java.io.File( "." ).getCanonicalPath();
        System.out.println("Current dir:"+current);
 String currentDir = System.getProperty("user.dir");
        System.out.println("Current dir using System:" +currentDir);

OutPut:

Current dir: C:\WINDOWS\system32
Current dir using System: C:\WINDOWS\system32

My output is not correct because C drive is not my current directory. Need help in this regard.

转载于:https://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java

  • 写回答

19条回答 默认 最新

  • 程序go 2011-02-02 05:37
    关注

    Current working directory is defined differently in different Java implementations. For certain version prior to Java 7 there was no consistent way to get the working directory. You could work around this by launching Java file with -D and defining a variable to hold the info

    Something like

    java -D com.mycompany.workingDir="%0"
    

    That's not quite right, but you get the idea. Then System.getProperty("com.mycompany.workingDir")...

    评论

报告相同问题?