You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用NetBeans运行Gradle项目时遭遇java.nio.file.AccessDeniedException异常

NetBeans 13运行Google Drive上传项目时遇权限拒绝错误(CMD执行正常)

按照教程操作,通过CMD执行gradle run可正常将文件上传至Google Drive,但使用NetBeans 13运行同一Gradle项目时,出现java.nio.file.AccessDeniedException错误,无法访问项目下的tokens目录。

错误日志

JAVA_HOME="C:\Program Files\Java\jdk-17.0.3"
cd C:\Users\User\Documents\NetBeansProjects\DriveProyect; .\gradlew.bat --configure-on-demand -x check run
Configuration on demand is an incubating feature.
> Task :compileJava UP-TO-DATE

> Task :processResources UP-TO-DATE

> Task :classes UP-TO-DATE

> Task :run FAILED
Exception in thread "main" java.nio.file.AccessDeniedException: C:\Users\User\Documents\NetBeansProjects\DriveProyect\tokens

    at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:89)
    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:96)
    at java.base/sun.nio.fs.WindowsAclFileAttributeView.getFileSecurity(WindowsAclFileAttributeView.java:89)
    at java.base/sun.nio.fs.WindowsAclFileAttributeView.getOwner(WindowsAclFileAttributeView.java:122)
    at java.base/sun.nio.fs.FileOwnerAttributeViewImpl.getOwner(FileOwnerAttributeViewImpl.java:91)
    at com.google.api.client.util.store.FileDataStoreFactory.setPermissionsToOwnerOnlyWindows(FileDataStoreFactory.java:157)
    at com.google.api.client.util.store.FileDataStoreFactory.<init>(FileDataStoreFactory.java:77)
    at DriveProyect.getCredentials(DriveProyect.java:65)
    at DriveProyect.main(DriveProyect.java:78)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command 'C:\Program Files\Java\jdk-17.0.3\bin\java.exe'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 894ms

相关代码(第65、78行所在方法)

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT)
      throws IOException {
    // Load client secrets.
    InputStream in = DriveProyect.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
    if (in == null) {
      throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
    }
    GoogleClientSecrets clientSecrets =
        GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
        .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
        .setAccessType("offline")
        .build();
    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    //returns an authorized Credential object.
    return credential;
  }

已确认信息

  • NetBeans 13的Gradle插件已正常安装

解决方案

1. 以管理员身份运行NetBeans

右键NetBeans快捷方式,选择以管理员身份运行,再执行项目。Windows下IDE可能因权限不足无法修改项目目录内的文件/文件夹。

2. 修改tokens目录权限

找到项目下的tokens目录(不存在则手动创建),右键选择属性 -> 安全 -> 编辑,给当前用户添加完全控制权限,应用后重新运行项目。

3. 更换令牌存储目录

避免在项目目录内存储令牌,改为用户目录下的路径,比如System.getProperty("user.home") + "/.credentials/drive-tokens"。修改代码中TOKENS_DIRECTORY_PATH的值:

private static final String TOKENS_DIRECTORY_PATH = System.getProperty("user.home") + "/.credentials/drive-tokens";

手动创建该目录并赋予当前用户权限,再重新运行。

4. 绕过权限检查或降级JDK

Google的FileDataStoreFactory在Windows下会尝试设置目录权限为仅所有者可访问,JDK 17可能存在兼容性问题。可通过自定义DataStoreFactory绕过权限检查,或降级JDK到11版本尝试。

内容的提问来源于stack exchange,提问作者syrup

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 09:15:36