如何获取JavaSwing项目的项目路径

如何获取JavaSwing项目的项目路径,java,swing,Java,Swing,我想将上传的文件存储在我的项目文件夹中,所以如何获取java swing项目的项目路径我通过使用这个 System.getProperty("user.dir") 我用这个 System.getProperty("user.dir") 我使用了以下代码来标识类文件的位置: private static File findProgramLocation() throws URISyntaxException { File file = new File(MyClass.class.ge

我想将上传的文件存储在我的项目文件夹中,所以如何获取java swing项目的项目路径

我通过使用这个

System.getProperty("user.dir")

我用这个

System.getProperty("user.dir")

我使用了以下代码来标识类文件的位置:

private static File findProgramLocation() throws URISyntaxException {
    File file = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
    if (!file.exists()) {
        throw new RuntimeException("Could not identify program location, found " + file.getAbsolutePath());
    }
    if (!file.isDirectory()) {
        // should be a JAR
        file = file.getParentFile();
    }
    return file;
}

它要么返回包含类文件的根文件夹,要么返回包含JAR文件的文件夹(如果已打包)。

我使用了以下代码来标识类文件的位置:

private static File findProgramLocation() throws URISyntaxException {
    File file = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
    if (!file.exists()) {
        throw new RuntimeException("Could not identify program location, found " + file.getAbsolutePath());
    }
    if (!file.isDirectory()) {
        // should be a JAR
        file = file.getParentFile();
    }
    return file;
}

它将返回包含类文件的根文件夹,或者返回包含JAR文件的文件夹(如果已打包)。

您是指当前执行应用程序的位置吗?您可以使用
newfile(“.”)和
getPath()
来确定当前的执行位置……您是说当前从哪个位置执行应用程序?您可以使用
新文件(“.”
并使用
getPath()
确定当前执行位置。。。