0

I'm trying to set permissions to a file because I'm getting warnings and a NoClassDefFoundError after that, I tried using

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

But doesn't work, the file is created this way:

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

This is for Google Calendar V3 API, i'm using a Mac, and here the project executes with no problem, but when I run it in a Windows 7 the problem start to show and the .jar crashes

The Exception seems to trigger when the file don't have the permissions.

this is the whole class

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");
 }
}

this is what I get when I try to run the jar enter image description here

user207421
  • 305,947
  • 44
  • 307
  • 483

1 Answers1

0

This is related to the problem that Java is unable to resolve the system property "user.home" on Windows 7. On Unix and Unix-like OS, this property value will be evaluated to be the environment variable $HOME such as /home/my_username. However, on Windows 7, Java does not interpret it to be the default user directory C:\User\my_username. More details can be found from the related post.

Community
  • 1
  • 1
tonga
  • 11,749
  • 25
  • 75
  • 96
  • I tried using user.dir and i'm still getting the same warnings. – Jose Miguel Ledón Aug 16 '14 at 02:17
  • like using C:\ as directory? I don't get what's 'hard coded' way – Jose Miguel Ledón Aug 16 '14 at 02:19
  • Yes, but in Java you need to put something like "C:\\\\". – tonga Aug 16 '14 at 02:21
  • Ok, if I was using System.getProperty("user.home"), ".store/calendar_sample" AND changed to "C:\\\\\\\\", ".store/calendar_sample" and I'm getting the same warning, It seems doesn't work – Jose Miguel Ledón Aug 16 '14 at 02:29
  • When you debugged your program, did you see the file `C:\.store\calendar_sample` is being created when you call that line? Also it should be "C:\\", just two back slashes. – tonga Aug 16 '14 at 02:33
  • yes, I debugged it and the directory and the file is created every time I run the project – Jose Miguel Ledón Aug 16 '14 at 02:36
  • I don't see any problems then based on your code. Can you post your full code? – tonga Aug 16 '14 at 02:38
  • Where does the NoClassDefFoundError occur from the stack trace? – tonga Aug 16 '14 at 02:44
  • in this line: return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user") in autorize() method, when the dataStoreFactory is not working well it throw the exception, in the Mac, it doesnt happen, I checked the classpath and all the jars are my lib folder too. – Jose Miguel Ledón Aug 16 '14 at 02:58