How to get the current working directory in Java?

前端 未结 22 3092
时光说笑
时光说笑 2020-11-21 07:16

I want to access my current working directory using java.

My code :

 String current = new java.io.File( \".\" ).getCanonicalPath();
        System.ou         


        
22条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 07:56

    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")...

提交回复
热议问题