使用带有java和scala的protobufs的问题 [英] Problems using protobufs with java and scala

查看:65
本文介绍了使用带有java和scala的protobufs的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件 xxx.proto 。我下载了protobuf编译器并安装了它。然后我发出了这个命令

I have a file xxx.proto. I downloaded the protobuf compiler and installed it. Then I issued this command

protoc --java_out=./ xxx.proto

它生成了我的xxx.java

and it generated my xxx.java

现在我想把这个文件编译成一个类我可以使用Scala的文件。

Now I want to compile this file into a class file which I can use with Scala.

javac xxx.java

这给了我这个错误

xxx.java:7: package com.google.protobuf does not exist
      com.google.protobuf.ExtensionRegistry registry) {
                         ^
xxx.java:12450: package com.google.protobuf.Descriptors does not exist
  private static com.google.protobuf.Descriptors.Descriptor
                                                ^
xxx.java:12453: package com.google.protobuf.GeneratedMessage does not exist
    com.google.protobuf.GeneratedMessage.FieldAccessorTable

...
...
...

100 errors

现在我猜,它没有包裹。

Now I guessed, it doesnt have the package.

因此,我将com.google.protobuf软件包的类文件复制到xxx.java所在的同一文件夹中。注意 - 我没有编译这个包。我从另一个包含jar文件的扩展中下载了jar。所以我提取了它们。现在我在xxx.java所在的当前路径有com / google / protobuf / * .class的protobuf库。

So I copied the class files of package com.google.protobuf into the same folder where xxx.java exists. Note - I didnt compile this package. I downloaded the jar from another extension which had the jar files. So I extracted them. Now my current path where xxx.java resides has com/google/protobuf/ *.class of protobuf library.

我再次发出了javac命令。

I issued the javac command again.

这次我得到了一组不同的错误 -

This time I got a different set of errors -

    xxx.java:10: cannot find symbol
    symbol  : class MessageOrBuilder
    location: package com.google.protobuf
          extends com.google.protobuf.MessageOrBuilder {
                                     ^
    xxx.java:215: cannot find symbol
    symbol  : class MessageOrBuilder
    location: package com.google.protobuf
            extends com.google.protobuf.MessageOrBuilder {
                                       ^
    xxx.java:608: cannot find symbol
    symbol  : class MessageOrBuilder
    location: package com.google.protobuf
            extends com.google.protobuf.MessageOrBuilder {
                                       ^
    xxx.java:1017: cannot find symbol
    symbol  : class MessageOrBuilder
    location: package com.google.protobuf
            extends com.google.protobuf.MessageOrBuilder {

..... 100 errors

我甚至试图编译google protobufs附带的源文件。生成的java类给出了相同的错误。

I even tried to compile the source files which came with google protobufs. The generated java classes are giving the same errors.

任何想法该怎么做?

回答

好的。谢谢大家。

主要问题是来自google的协议缓冲编译器包默认情况下创建java库。我认为它确实安装了它。如果你正在运行Maven,它确实会这样做。但我没有maven

The main problem is that protocol buffers compiler package from google doesnt by default create the java library. I assumed that it does and installs it. It actually does if you are running Maven. But i didnt have maven

所以我编译了 / java / src 中的代码并使用了jar。
^

So i compiled the code in /java/src and used the jar. ^

推荐答案

编译时,需要在类路径上使用protobuf lib。所有缺少的包和类都来自protobuf lib。

When compiling, you need to have protobuf lib on your classpath. All those missing packages and classes are from protobuf lib.

查找protobuf jar并使用

Find protobuf jar and use

javac -cp path/to/protobuf.jar xxx.java

这篇关于使用带有java和scala的protobufs的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆