Rajesh Surana
Rajesh Surana

Reputation: 932

File not found error for sample program in Java on Freebase API documentation

I am trying the sample program in Java given here in the Freebase documentation.

Here is the program

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.jayway.jsonpath.JsonPath;
import java.io.FileInputStream;
import java.util.Properties;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class TopicSample {
  public static Properties properties = new Properties();
  public static void main(String[] args) {
    try {
      properties.load(new FileInputStream("freebase.properties"));
      HttpTransport httpTransport = new NetHttpTransport();
      HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
      JSONParser parser = new JSONParser();
      String query = "[{\"id\":null,\"name\":null,\"type\":\"/astronomy/planet\"}]";
      GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/mqlread");
      url.put("query", query);
      url.put("key", properties.get("API_KEY"));
      HttpRequest request = requestFactory.buildGetRequest(url);
      HttpResponse httpResponse = request.execute();
      JSONObject response = (JSONObject)parser.parse(httpResponse.parseAsString());
      JSONArray results = (JSONArray)response.get("result");
      for (Object result : results) {
        System.out.println(JsonPath.read(result,"$.name").toString());
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

I have also replaced "API_KEY" with my own generated key.

When I run this program I am getting an error

java.io.FileNotFoundException: freebase.properties (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at code.TopicSample.main(TopicSample.java:29)
BUILD SUCCESSFUL (total time: 0 seconds)

Do I have to create "freebase.properties" file? What would be the content of that file? I have skimmed through almost the complete documentation of the Freebase API but I could not find any clue about this file. Is this file available for download? I would appreciate a link to the information about this file.

Upvotes: 0

Views: 143

Answers (1)

Fluffmeister General
Fluffmeister General

Reputation: 383

Yes, by the looks of it. You'll notice you have a call to properties.get("API_KEY") - so presumably you need to put your API_KEY in the properties file.

So, a file called freebase.properties containing:

API_KEY = <your actual API key>

Upvotes: 1

Related Questions