Sitelet https://github.com/googleapis/google-cloud-java/issues/34
Skip to content

What's the best way for a total n00b to start with this library? #34

Description

@jgeewax

I'm having trouble getting a HelloDatastore project working. Hoping that you can help me get this going...

Here's what I did:

$ git clone https://github.com/GoogleCloudPlatform/gcloud-java.git
$ cd gcloud-java
$ sudo apt-get install maven
$ mvn install
$ export CLASSPATH=~/gcdjavasample/gcloud-java/target/gcloud-java-0.0.3.jar

Here's the file I'm trying to run (HelloDatastore.java):

import com.google.gcloud.datastore.DatastoreService;
import com.google.gcloud.datastore.DatastoreServiceFactory;
import com.google.gcloud.datastore.DatastoreServiceOptions;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;



public class HelloDatastore {
  private static final String DATASET = "gcloud-datastore-demo";

  public static void main(String[] args) {
    DatastoreServiceOptions options = DatastoreServiceOptions.builder().dataset(DATASET).build();
    DatastoreService datastore = DatastoreServiceFactory.getDefault(options);
    KeyFactory keyFactory = datastore.newKeyFactory().kind("Person");
    Key key = keyFactory.newKey("Jimmy");

    System.out.println("Trying to get the entity by its key!");

    Entity entity = datastore.get(key);

    if (entity == null) {
      System.out.println("Entity not found! Creating it!");
      entity = Entity.builder(key)
          .set("name", "John Doe")
          .set("age", 30)
          .set("updated", false)
          .build();
      datastore.put(entity);
    } else {
      System.out.println("Entity found! Updating it!");
      boolean updated = entity.getBoolean("updated");
      if (!updated) {
        String[] name = entity.getString("name").split(" ");
        entity = Entity.builder(entity)
            .set("name", name[0])
            .set("last_name", name[1])
            .set("updated", true)
            .remove("old_property")
            .set("new_property", 1.1)
            .build();
        datastore.update(entity);
      }
    }

    System.out.println("Done!");
  }
}

I can get it to compile just fine, then I run into problems running the class. Apparently I need Guava? And google-auth-library-java ? And google-api-java-client ?

What exactly is the best way to get this so that I can run... java HelloDatastore and I'll see some stuff happening.... ?

(Sorry for the dumb question....)

Activity

  1. added this to the milestone on Mar 19, 2015
  2. aozarov commented on Mar 19, 2015

    @aozarov
    Contributor

    When using maven one's artifact does not include/bundle all its dependencies.
    There are various ways to deal with it but the most recommended way for someone that consumes
    a maven artifact[s] is to make its code using maven (http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html) and adding gcloud-java as a dependency as such:

    <dependency>
      <groupId>com.google.gcloud</groupId>
      <artifactId>gcloud-java</artifactId>
      <version>0.0.3</version>
    </dependency>

    For latest versions one could use: http://search.maven.org/#search%7Cga%7C1%7Cgcloud-java
    If you want to run your code (not as a test via "mvn test") you have various options. here are some references:
    http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/
    http://mojo.codehaus.org/exec-maven-plugin/examples/example-exec-for-java-programs.html

    There are other options such as using ant and ivy (http://ant.apache.org/ivy/history/2.2.0/tutorial/start.html) or using tools or various maven plugins to bundle all jars (latter is not recommended).

  3. aozarov commented on Mar 20, 2015

    @aozarov
    Contributor

    @jgeewax let me know what you want to do about this one?

  4. assigned and unassigned on Mar 20, 2015
  5. jgeewax commented on Mar 23, 2015

    @jgeewax
    Author

    Is there any value is having a "starter-project" template (not the expense report example -- but one that is just a blank template) ?

  6. assigned and unassigned on Mar 23, 2015
  7. aozarov commented on Mar 23, 2015

    @aozarov
    Contributor

    I think little value. All 3 main IDEs have an option for creating a "starter" maven project and an easy way to add dependencies. Yes, we could create a simple "hello gcloud java" simple project with maven, ant and gradle config files (already configured to pull gcloud-java) (we could also provide project configuration for all 3 main IDEs but I am not sure that is a great idea, especially when configuration may differ between IDEs versions) but I think one could also clone the "expense report" project (when available) and trim the unnecessary files. In any case, I don't object the idea (but not sure where exactly to put it - see: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html -- as I don't think we need to create another gitHub project for it).

  8. assigned and unassigned on Mar 23, 2015
  9. 18 remaining items

  10. added a commit that references this issue on Feb 24, 2026
  11. added a commit that references this issue on Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions