Search code examples
javagoogle-cloud-platformavropublish-subscribegoogle-cloud-pubsub

Running sample Google Maven project in Google Cloud Shell


I am brand new to GCP world. I am trying to run one of the pubsub examples (https://github.com/googleapis/java-pubsub/blob/HEAD/samples/snippets/src/main/java/pubsub/SubscribeWithAvroSchemaExample.java) in Google's Github repo on Cloud Shell.

Their documentation also has links to Cloud Shell (https://github.com/googleapis/java-pubsub/tree/ff9c9c15a9efb10d5cbc9328c7a703a20b5d4b44#samples)

I am able to build on the cloud shell by running mvn clean install. But when I run that Java class SubscribeWithAvroSchemaExample.java (after filling in the project id and subscrption id), I get the below error

satish_anupindi84@cloudshell:~$ cd '/home/satish_anupindi84' && env '/usr/lib/jvm/java-11-openjdk-amd64/bin/java' '-Dfile.encoding=UTF-8' '-cp' '/home/satish_anupindi84/.theia/workspace-storage/e742f48ad7fde7236560e8cf9e48d278/redhat.java/jdt_ws/jdt.ls-java-project/bin' 'pubsub.SubscribeWithAvroSchemaExample' Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at pubsub.SubscribeWithAvroSchemaExample.main(SubscribeWithAvroSchemaExample.java:39)

Would someone try this out and let me know what they get?

Ran it manually, and did not get any headway

satish_anupindi84@cloudshell:~/cloudshell_open/java-pubsub-2$ cd samples/snippets/ satish_anupindi84@cloudshell:~/cloudshell_open/java-pubsub-2/samples/snippets$ env '/usr/lib/jvm/java-11-openjdk-amd64/bin/java' '-Dfile-encoding=UTF-8' 'pubsub.SubscribeWithAvroSchemaExample' Error: Could not find or load main class pubsub.SubscribeWithAvroSchemaExample Caused by: java.lang.ClassNotFoundException: pubsub.SubscribeWithAvroSchemaExample

Attached screenshot. enter image description here


Solution

  • I cannot reproduce this issue. My guess is that when you ran mvn clean install, it also ran the tests included in the snippets. Some of those tests can take time and may fail, which can ruin the build process. If you simply want to test a snippet, then you can just skip those tests.

    My suggestion is to add a flag to skip tests:

    UPDATE: Check your current directory. Make sure to run this command at your $HOME/cloudshell_open/java-pubsub-3/samples/snippets.

    mvn clean install -DskipTests
    

    Afterwards, make sure the build returns success and then rerun the sample via Cloud Editor.

    The steps I did:

    1. Open a Terminal.
    2. Clone repo to $HOME dir: git clone https://github.com/googleapis/java-pubsub.git
    3. cd java-pubsub/samples/snippets
    4. mvn clean install -DskipTests build result should look like this:

    enter image description here

    1. Open samples/snippets workspace at Cloud Shell Editor.
    2. Go to your preferred class and click Run button near the main method. Right Click and Run should also work.

    I was able to make it work properly. Here's a screenshot:

    enter image description here