How to get the current working directory in Java?

前端 未结 22 3077
时光说笑
时光说笑 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 08:09

    What makes you think that c:\windows\system32 is not your current directory? The user.dir property is explicitly to be "User's current working directory".

    To put it another way, unless you start Java from the command line, c:\windows\system32 probably is your CWD. That is, if you are double-clicking to start your program, the CWD is unlikely to be the directory that you are double clicking from.

    Edit: It appears that this is only true for old windows and/or Java versions.

    0 讨论(0)
  • 2020-11-21 08:11

    I'm on Linux and get same result for both of these approaches:

    @Test
    public void aaa()
    {
        System.err.println(Paths.get("").toAbsolutePath().toString());
    
        System.err.println(System.getProperty("user.dir"));
    }
    

    Paths.get("") docs

    System.getProperty("user.dir") docs

    0 讨论(0)
  • 2020-11-21 08:11

    On Linux when you run a jar file from terminal, these both will return the same String: "/home/CurrentUser", no matter, where youre jar file is. It depends just on what current directory are you using with your terminal, when you start the jar file.

    Paths.get("").toAbsolutePath().toString();
    
    System.getProperty("user.dir");
    

    If your Class with main would be called MainClass, then try:

    MainClass.class.getProtectionDomain().getCodeSource().getLocation().getFile();
    

    This will return a String with absolute path of the jar file.

    0 讨论(0)
  • 2020-11-21 08:13

    This isn't exactly what's asked, but here's an important note: When running Java on a Windows machine, the Oracle installer puts a "java.exe" into C:\Windows\system32, and this is what acts as the launcher for the Java application (UNLESS there's a java.exe earlier in the PATH, and the Java app is run from the command-line). This is why File(".") keeps returning C:\Windows\system32, and why running examples from macOS or *nix implementations keep coming back with different results from Windows.

    Unfortunately, there's really no universally correct answer to this one, as far as I have found in twenty years of Java coding unless you want to create your own native launcher executable using JNI Invocation, and get the current working directory from the native launcher code when it's launched. Everything else is going to have at least some nuance that could break under certain situations.

    0 讨论(0)
  • 2020-11-21 08:13

    System.getProperty("java.class.path")

    0 讨论(0)
  • 2020-11-21 08:14

    This will give you the path of your current working directory:

    Path path = FileSystems.getDefault().getPath(".");
    

    And this will give you the path to a file called "Foo.txt" in the working directory:

    Path path = FileSystems.getDefault().getPath("Foo.txt");
    

    Edit : To obtain an absolute path of current directory:

    Path path = FileSystems.getDefault().getPath(".").toAbsolutePath();
    

    * Update * To get current working directory:

    Path path = FileSystems.getDefault().getPath("").toAbsolutePath();
    
    0 讨论(0)
提交回复
热议问题