1248

我想使用 Java 访问我当前的工作目录。

我的代码:

 String currentPath = new java.io.File(".").getCanonicalPath();
 System.out.println("Current dir:" + currentPath);

 String currentDir = System.getProperty("user.dir");
 System.out.println("Current dir using System:" + currentDir);

输出:

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

我的输出不正确,因为 C 驱动器不是我的当前目录。

如何获取当前目录?

4

25 回答 25

1365

代码 :

public class JavaApplication {
  public static void main(String[] args) {
    System.out.println("Working Directory = " + System.getProperty("user.dir"));
  }
}

这将打印初始化应用程序的当前目录的绝对路径。


解释:

文档中:

java.io包使用当前用户目录解析相对路径名。当前目录表示为系统属性,即user.dirJVM 被调用的目录。

于 2011-09-29T21:12:33.577 回答
475

请参阅:路径操作(Java™ 教程 > 基本类 > 基本 I/O)

使用java.nio.file.Pathand java.nio.file.Paths,您可以执行以下操作来显示 Java 认为您当前的路径。这适用于 7 及更高版本,并使用 NIO。

Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current absolute path is: " + s);

这输出:

Current absolute path is: /Users/george/NetBeansProjects/Tutorials

就我而言,这就是我上课的地方。

以相对方式构建路径,通过不使用前导分隔符来指示您正在构建绝对路径,将使用此相对路径作为起点。

于 2013-04-11T17:10:25.723 回答
251

以下适用于 Java 7 及更高版本(有关文档,请参见此处)。

import java.nio.file.Paths;

Paths.get(".").toAbsolutePath().normalize().toString();
于 2014-03-12T12:03:01.137 回答
79

这将为您提供当前工作目录的路径:

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

这将为您提供工作目录中名为“Foo.txt”的文件的路径:

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

编辑: 获取当前目录的绝对路径:

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

* 更新 * 获取当前工作目录:

Path path = FileSystems.getDefault().getPath("").toAbsolutePath();
于 2017-09-15T20:01:58.207 回答
60

Java 11 及更新版本

此解决方案比其他解决方案更好,更便携:

Path cwd = Path.of("").toAbsolutePath();

甚至

String cwd = Path.of("").toAbsolutePath().toString();
于 2018-06-25T16:29:29.377 回答
35

这是我的解决方案

File currentDir = new File("");
于 2013-05-28T23:09:41.913 回答
30

是什么让您认为c:\windows\system32不是您的当前目录?该user.dir属性明确为“用户的当前工作目录”。

换句话说,除非您从命令行启动 Java,否则c:\windows\system32可能是您的 CWD。也就是说,如果您双击启动程序,则 CWD 不太可能是您从中双击的目录。

编辑:这似乎只适用于旧 Windows 和/或 Java 版本。

于 2011-02-02T05:39:21.080 回答
26

使用CodeSource#getLocation().

这在 JAR 文件中也可以正常工作。您可以通过 获得CodeSourceProtectionDomain#getCodeSource()ProtectionDomain反过来又可以通过 获得Class#getProtectionDomain()

public class Test {
    public static void main(String... args) throws Exception {
        URL location = Test.class.getProtectionDomain().getCodeSource().getLocation();
        System.out.println(location.getFile());
    }
}
于 2014-01-22T05:20:45.633 回答
23
this.getClass().getClassLoader().getResource("").getPath()
于 2013-05-15T13:50:15.700 回答
19

通常,作为 File 对象:

File getCwd() {
  return new File("").getAbsoluteFile();
}

您可能希望拥有像 "D:/a/b/c" 这样的完整限定字符串:

getCwd().getAbsolutePath()
于 2015-01-26T12:13:23.803 回答
14

我在 Linux 上,这两种方法都得到了相同的结果:

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

    System.err.println(System.getProperty("user.dir"));
}

Paths.get("")文档

System.getProperty("user.dir")文档

于 2013-12-28T03:11:13.113 回答
6

我希望你想访问包括包的当前目录,即如果你的 Java 程序在c:\myApp\com\foo\src\service\MyTest.java其中并且你想打印到c:\myApp\com\foo\src\service那时你可以尝试以下代码:

String myCurrentDir = System.getProperty("user.dir")
            + File.separator
            + System.getProperty("sun.java.command")
                    .substring(0, System.getProperty("sun.java.command").lastIndexOf("."))
                    .replace(".", File.separator);
    System.out.println(myCurrentDir);

注意:此代码仅在 Windows 中使用 Oracle JRE 进行测试。

于 2012-09-27T10:09:19.607 回答
5

Linux 上,当您从终端运行jar文件时,无论您的 jar 文件在哪里,它们都将返回相同的:“/home/CurrentUser” 。当您启动 jar 文件时,这仅取决于您在终端上使用的当前目录。String

Paths.get("").toAbsolutePath().toString();

System.getProperty("user.dir");

如果您的Classwithmain会被调用MainClass,请尝试:

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

这将返回一个String带有绝对路径jar文件。

于 2015-12-17T22:29:08.303 回答
5

使用 Windows user.dir 按预期返回目录,但不是当您以提升的权限(以管理员身份运行)启动应用程序时,在这种情况下您会得到 C:\WINDOWS\system32

于 2015-12-25T14:42:46.937 回答
4

提到它只被签入,Windows但我认为它在其他操作系统上完美运行 [ Linux,MacOs,Solaris] :)。


我在同一个目录中有2 个文件。 .jar我想从一个.jar文件中启动.jar位于同一目录中的另一个文件。

问题是当你从cmd当前目录启动它时是system32.


警告!

  • 以下似乎在我所做的所有测试中都运行良好,即使使用文件夹名称;][[;'57f2g34g87-8+9-09!2#@!$%^^&()()%&$%^@# 运行良好。
  • 我正在使用ProcessBuilder以下内容:

..

//The class from which i called this was the class `Main`
String path = getBasePathForClass(Main.class);
String applicationPath=  new File(path + "application.jar").getAbsolutePath();


System.out.println("Directory Path is : "+applicationPath);

//Your know try catch here
//Mention that sometimes it doesn't work for example with folder `;][[;'57f2g34g87-8+9-09!2#@!$%^^&()` 
ProcessBuilder builder = new ProcessBuilder("java", "-jar", applicationPath);
builder.redirectErrorStream(true);
Process process = builder.start();

//...code

getBasePathForClass(Class<?> classs)

    /**
     * Returns the absolute path of the current directory in which the given
     * class
     * file is.
     * 
     * @param classs
     * @return The absolute path of the current directory in which the class
     *         file is.
     * @author GOXR3PLUS[StackOverFlow user] + bachden [StackOverFlow user]
     */
    public static final String getBasePathForClass(Class<?> classs) {

        // Local variables
        File file;
        String basePath = "";
        boolean failed = false;

        // Let's give a first try
        try {
            file = new File(classs.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());

            if (file.isFile() || file.getPath().endsWith(".jar") || file.getPath().endsWith(".zip")) {
                basePath = file.getParent();
            } else {
                basePath = file.getPath();
            }
        } catch (URISyntaxException ex) {
            failed = true;
            Logger.getLogger(classs.getName()).log(Level.WARNING,
                    "Cannot firgue out base path for class with way (1): ", ex);
        }

        // The above failed?
        if (failed) {
            try {
                file = new File(classs.getClassLoader().getResource("").toURI().getPath());
                basePath = file.getAbsolutePath();

                // the below is for testing purposes...
                // starts with File.separator?
                // String l = local.replaceFirst("[" + File.separator +
                // "/\\\\]", "")
            } catch (URISyntaxException ex) {
                Logger.getLogger(classs.getName()).log(Level.WARNING,
                        "Cannot firgue out base path for class with way (2): ", ex);
            }
        }

        // fix to run inside eclipse
        if (basePath.endsWith(File.separator + "lib") || basePath.endsWith(File.separator + "bin")
                || basePath.endsWith("bin" + File.separator) || basePath.endsWith("lib" + File.separator)) {
            basePath = basePath.substring(0, basePath.length() - 4);
        }
        // fix to run inside netbeans
        if (basePath.endsWith(File.separator + "build" + File.separator + "classes")) {
            basePath = basePath.substring(0, basePath.length() - 14);
        }
        // end fix
        if (!basePath.endsWith(File.separator)) {
            basePath = basePath + File.separator;
        }
        return basePath;
    }
于 2016-11-20T17:04:33.930 回答
2

假设您正在尝试在 eclipse 或 netbean 中运行您的项目,或者从命令行独立运行。我已经写了一个方法来修复它

public static final String getBasePathForClass(Class<?> clazz) {
    File file;
    try {
        String basePath = null;
        file = new File(clazz.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
        if (file.isFile() || file.getPath().endsWith(".jar") || file.getPath().endsWith(".zip")) {
            basePath = file.getParent();
        } else {
            basePath = file.getPath();
        }
        // fix to run inside eclipse
        if (basePath.endsWith(File.separator + "lib") || basePath.endsWith(File.separator + "bin")
                || basePath.endsWith("bin" + File.separator) || basePath.endsWith("lib" + File.separator)) {
            basePath = basePath.substring(0, basePath.length() - 4);
        }
        // fix to run inside netbean
        if (basePath.endsWith(File.separator + "build" + File.separator + "classes")) {
            basePath = basePath.substring(0, basePath.length() - 14);
        }
        // end fix
        if (!basePath.endsWith(File.separator)) {
            basePath = basePath + File.separator;
        }
        return basePath;
    } catch (URISyntaxException e) {
        throw new RuntimeException("Cannot firgue out base path for class: " + clazz.getName());
    }
}

要使用,无论你想获得读取文件的基本路径,你可以将你的锚类传递给上面的方法,结果可能是你需要的东西:D

最好的,

于 2015-04-16T04:39:23.050 回答
0

当前工作目录在不同的 Java 实现中定义不同。对于 Java 7 之前的某些版本,没有一致的方法来获取工作目录。您可以通过启动 Java 文件-D并定义一个变量来保存信息来解决此问题

就像是

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

这不太对,但你明白了。然后System.getProperty("com.mycompany.workingDir")...

于 2011-02-02T05:37:54.433 回答
0

尝试这样的事情我知道我迟到了答案,但是这个明显的事情发生在 java8 一个新版本中,从那里提出这个问题但是..

编码

import java.io.File;

public class Find_this_dir {

    public static void main(String[] args) {

//some sort of a bug in java path is correct but file dose not exist
        File this_dir = new File("");

//but these both commands work too to get current dir        
//      File this_dir_2 = new File(this_dir.getAbsolutePath());
        File this_dir_2 = new File(new File("").getAbsolutePath());

        System.out.println("new File(" + "\"\"" + ")");
        System.out.println(this_dir.getAbsolutePath());
        System.out.println(this_dir.exists());
        System.out.println("");
        System.out.println("new File(" + "new File(" + "\"\"" + ").getAbsolutePath()" + ")");
        System.out.println(this_dir_2.getAbsolutePath());
        System.out.println(this_dir_2.exists());
    }
}

这将起作用并向您显示当前路径,但我现在不知道为什么 java 无法找到当前目录,new File("");除了我使用的是 Java8 编译器...

这很好用,我什至测试过new File(new File("").getAbsolutePath());

现在您在 File 对象中有当前目录(示例文件对象是 f 然后),

f.getAbsolutePath()将为您提供 String 变量类型的路径...

在不是驱动器 C 的另一个目录中测试工作正常

于 2021-01-25T06:53:00.923 回答
0

你可能会使用new File("./"). 这种方式isDirectory()返回 true(至少在 Windows 平台上)。另一方面new File("") isDirectory()返回假。

于 2022-01-07T16:58:07.270 回答
0

当混乱的时刻冒出来时,这是我的灵丹妙药。(将其称为主要的第一件事)。也许例如 JVM 被 IDE 滑入不同的版本。此静态函数搜索当前进程 PID 并在该 pid 上打开 VisualVM。混乱就停在那里,因为你想要它并且你得到它......

  public static void callJVisualVM() {
    System.out.println("USER:DIR!:" + System.getProperty("user.dir"));
    //next search current jdk/jre
    String jre_root = null;
    String start = "vir";
    try {
        java.lang.management.RuntimeMXBean runtime =
                java.lang.management.ManagementFactory.getRuntimeMXBean();
        String jvmName = runtime.getName();
        System.out.println("JVM Name = " + jvmName);
        long pid = Long.valueOf(jvmName.split("@")[0]);
        System.out.println("JVM PID  = " + pid);
        Runtime thisRun = Runtime.getRuntime();
        jre_root = System.getProperty("java.home");
        System.out.println("jre_root:" + jre_root);
        start = jre_root.concat("\\..\\bin\\jvisualvm.exe " + "--openpid " + pid);
        thisRun.exec(start);
    } catch (Exception e) {
        System.getProperties().list(System.out);
        e.printStackTrace();
    }
}
于 2020-05-18T00:54:45.787 回答
0

对于 Java 11,您还可以使用:

var path = Path.of(".").toRealPath();
于 2021-03-24T17:52:20.060 回答
0

这并不是所要求的,但这里有一个重要说明:在 Windows 机器上运行 Java 时,Oracle 安装程序会将“java.exe”放入 C:\Windows\system32,这就是作为Java 应用程序(除非 PATH 中有一个 java.exe,并且 Java 应用程序是从命令行运行的)。这就是为什么 File(".") 不断返回 C:\Windows\system32,以及为什么从 macOS 或 *nix 实现中运行的示例不断返回来自 Windows 的不同结果的原因。

不幸的是,据我在 20 年的 Java 编码中发现,这确实没有普遍正确的答案,除非您想使用 JNI 调用创建自己的本机启动器可执行文件,并在何时从本机启动器代码中获取当前工作目录它启动了。其他一切都将至少有一些细微差别,在某些情况下可能会破裂。

于 2020-09-15T22:20:22.507 回答
0

我最喜欢的方法是从附加到当前运行进程的系统环境变量中获取它。在这种情况下,您的应用程序由 JVM 管理。

String currentDir = System.getenv("PWD");
/*
 /home/$User/Documents/java
*/

要查看您可能会发现有用的其他环境变量,例如主目录、操作系统版本......

//Home directory
String HomeDir = System.getEnv("HOME");


//Outputs for unix
/home/$USER

//Device user
String user = System.getEnv("USERNAME");


//Outputs for unix
$USER

这种方法的美妙之处在于,所有类型的操作系统平台的所有路径都将得到解决

于 2021-05-31T10:10:36.037 回答
-1

这里发布的答案都不适合我。这是起作用的:

java.nio.file.Paths.get(
  getClass().getProtectionDomain().getCodeSource().getLocation().toURI()
);

编辑:我的代码中的最终版本:

URL myURL = getClass().getProtectionDomain().getCodeSource().getLocation();
java.net.URI myURI = null;
try {
    myURI = myURL.toURI();
} catch (URISyntaxException e1) 
{}
return java.nio.file.Paths.get(myURI).toFile().toString()
于 2015-03-31T19:35:50.263 回答
-6

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

于 2015-01-03T23:10:09.710 回答