如何在Java的主文件夹(或Windows的appdata文件夹)中获得写权限? [英] How do I get write permissions in the home folder (or the appdata folder, for windows) in Java?

查看:466
本文介绍了如何在Java的主文件夹(或Windows的appdata文件夹)中获得写权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一些用户设置保存在home/appdata文件夹中,但是当我使用dir.canwrite()时,它返回false.这是我用来确定home/appdata文件夹的代码:

I am trying to save some user settings in the home/appdata folder, but when I use dir.canwrite() it returns false. This is the code I use to determine the home/appdata folder:

public static String getAppDataPath() {
    if (System.getProperty("os.name").contains("Windows")) {
        return System.getenv("APPDATA");
    } else {
        return getUnixHome();
    }
}

public static String getUnixHome() {
    String home = System.getProperty("user.home");
    return home != null ? home : "~";
}

这是尝试执行mkdir的代码:

And this is the code trying to mkdir:

public static boolean checkExistenceDir(String path) {
    File dir = new File(path);
    if(!dir.exists()) {
        dir.mkdir();
    }
}

有问题的路径是:

getAppDataPath() + ".foo" + File.separatorChar

推荐答案

您需要以管理员身份启动Java进程.

You need the Java process to be started as an Administrator.

您可以在Windows中创建run.vbs脚本来启动jar :

Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "cmd.exe", , , "runas", 1
oShell.Run "java -jar myjar.jar"

要使程序始终以管理员身份运行,您需要使其至少以管理员身份运行一次并更新注册表项.

To make the program always run as administrator, you need it to run as administrator at least once and update a registry key.

这篇关于如何在Java的主文件夹(或Windows的appdata文件夹)中获得写权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆