【问题标题】:ClassNotFoundException HttpServletRequest when changing permissions for everybody, java.io.FileClassNotFoundException HttpServletRequest 更改每个人的权限时,java.io.File
【发布时间】:2014-08-16 01:23:28
【问题描述】:

我正在尝试设置文件的权限,因为我收到警告和 NoClassDefFoundError 之后,我尝试使用

file.setWritable(true,false);
file.setExecutable(true,false);
file.setReadable(true,false);

但是不行,文件是这样创建的:

private static final java.io.File DATA_STORE_DIR
        = new java.io.File(System.getProperty("user.home"), ".store/calendar_sample");

这是用于 Google Calendar V3 API,我使用的是 Mac,这里项目执行没有问题,但是当我在 Windows 7 中运行它时,问题开始出现并且 .jar 崩溃

当文件没有权限时似乎会触发异常。

这是全班

public class GoogleCalendar2 {

private static final String APPLICATION_NAME = "expeDiente";
private static final java.io.File DATA_STORE_DIR
        = new java.io.File("C:\\\\\\\\", ".store/calendar_sample");
private static FileDataStoreFactory dataStoreFactory;
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static HttpTransport httpTransport;
@SuppressWarnings("unused")
private static Calendar client;
String id = "";

public GoogleCalendar2() throws GeneralSecurityException, IOException, Exception {
    DATA_STORE_DIR.setWritable(true, false);
    DATA_STORE_DIR.setExecutable(true, false);
    DATA_STORE_DIR.setReadable(true, false);

    httpTransport = GoogleNetHttpTransport.newTrustedTransport();

    dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

    Credential credential = authorize();

    client = new Calendar.Builder(httpTransport, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME).build();

}

/**
 * Authorizes the installed application to access user's protected data.
 */
private static Credential authorize() throws Exception {
    Set<String> scopes = new HashSet<String>();
    scopes.add(CalendarScopes.CALENDAR);
    scopes.add(CalendarScopes.CALENDAR_READONLY);

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            httpTransport, JSON_FACTORY, "user id google", "user secrets", scopes)
            .setDataStoreFactory(dataStoreFactory)
            .build();
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
 }
}

这是我尝试运行 jar 时得到的

【问题讨论】:

    标签: java file


    【解决方案1】:

    这与Java在Windows 7上无法解析系统属性"user.home"的问题有关。在Unix和类Unix操作系统上,该属性值将被评估为环境变量$HOME如@ 987654324@。但是,在 Windows 7 上,Java 不会将其解释为默认用户目录 C:\User\my_username。更多细节可以从相关post中找到。

    【讨论】:

    • 我尝试使用 user.dir,但仍然收到相同的警告。
    • 喜欢使用 C:\ 作为目录吗?我不明白什么是“硬编码”方式
    • 是的,但是在 Java 中你需要输入类似 "C:\\\\" 的内容。
    • 好的,如果我使用 System.getProperty("user.home"), ".store/calendar_sample" 并更改为 "C:\\\\\\\\", ".store /calendar_sample" 并且我收到相同的警告,它似乎不起作用
    • 当您调试程序时,您是否看到在调用该行时正在创建文件C:\.store\calendar_sample?它也应该是“C:\\”,只有两个反斜杠。
    猜你喜欢
    • 2023-02-18
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多