有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在Google Cloud Shell中运行示例Google Maven项目

我是全新的GCP世界。我正在尝试运行Google的Github-repo on Cloud Shell中的一个pubsub示例(https://github.com/googleapis/java-pubsub/blob/HEAD/samples/snippets/src/main/java/pubsub/SubscribeWithAvroSchemaExample.java

他们的文档还有到Cloud Shell(https://github.com/googleapis/java-pubsub/tree/ff9c9c15a9efb10d5cbc9328c7a703a20b5d4b44#samples)的链接

我可以通过运行mvn clean install在云shell上构建。但是当我运行Java类时,我看到了一个例子。java(在填写项目id和子选项id之后),我得到以下错误

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)

有人能试试这个,让我知道他们得到了什么吗

手动运行,没有任何进展

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

附上截图。 enter image description here


共 (1) 个答案

  1. # 1 楼答案

    我无法复制这个问题。我的猜测是,当您运行mvn clean install时,它也运行了代码片段中包含的测试。其中一些测试可能需要时间并且可能会失败,这可能会破坏构建过程。如果您只是想测试一个代码段,那么您可以跳过这些测试

    我的建议是添加一个跳过测试的标志:

    更新:检查当前目录。确保在$HOME/cloudshell_open/java-pubsub-3/samples/snippets上运行此命令

    mvn clean install -DskipTests
    

    之后,确保构建返回成功,然后通过云编辑器重新运行示例

    我采取的步骤是:

    1. 打开一个终端
    2. 将repo克隆到$HOME目录:git clone https://github.com/googleapis/java-pubsub.git
    3. cd java-pubsub/samples/snippets
    4. mvn clean install -DskipTests生成结果应如下所示:

    enter image description here

    1. 在Cloud Shell编辑器中打开samples/snippets工作区
    2. 转到您喜欢的课程,然后单击主方法附近的运行按钮。右键单击并运行也应该有效

    我能使它正常工作。以下是一个屏幕截图:

    enter image description here