I'm trying to make the Google Drive Java Quickstart work, but I'm stuck with a NullPointerException when trying to use the InputStream.
Google's quickstart code
InputStream in =
Quickstart.class.getResourceAsStream("client_secret.json");
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
According to one SO question, the getResourceAsStream looks for the file in the same directory as the class calling it, so I put my client_secret.json with my class:
|src/main/java
|---quickstart
|------Quickstart.java
|------client_secret.json
I still have the NullPointerException, so I tried to create the InputStream from a File instead of using getResourceAsStream:
java.io.File initialFile = new java.io.File("A:/Documents/client_secret.json");
// java.io.File initialFile = new java.io.File("client_secret.json");
if (initialFile.canRead()) {
System.out.println("it works!");
} else {
System.out.println("that's a fail");
}
System.out.println(initialFile.getAbsolutePath() + " " + initialFile.exists() + " " + initialFile.getAbsoluteFile().exists());
InputStream in = new FileInputStream(initialFile);
which results in:
that's a fail
A:\Documents\client_secret.json false false
Exception in thread "main" java.io.FileNotFoundException: A:\Documents\client_secret.json (Le fichier spécifié est introuvable)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at quickstart.Quickstart.authorize(Quickstart.java:76)
at quickstart.Quickstart.getDriveService(Quickstart.java:102)
at quickstart.Quickstart.main(Quickstart.java:111)
I can find the files when I use the path in my explorer.
I've tried different ways to open the file (absolute path, relative path, using "parent" constructor, ...), but always the same result. This code works when I execute it on online java compilers though (like tutorialspoint's one).
My OS is Windows 10, I'm working with Eclipse IDE.