Friday, October 11, 2013

Java Generated Code - Protocol Buffers



1) Download protobuf from protobuf website.

2) Install gcc and gcc-c++
 > sudo yum install gcc gcc-c++

3) Install Protocol Buffer Compiler using C++ compiler.(You can read the README file of protobuf)
./configure
make 
make check
make install

4) Generate Java file
A. now we have test.proto file, it is:

message ProgrammerInfo
{
    required string name = 1;
    required bool likeCoding = 2;

    enum CodingLanguage{
        JAVA = 0;
        PERL = 1;
        JAVASCRIPT = 2;
    }

    required CodingLanguage programLang = 3 [default = JAVA];
}

B. command to generate java file:
protoc -I=/home/haifzhan/sourceFolder --java_out=/home/haifzhan/outputFolder /home/haifzhan/sourceFolder/test.proto

Now the file called Test.java is generated.

How to use it?

ProgrammerInfo info = ProgrammerInfo.newBuilder().setName("haifzhan")
                            .setLikeCoding(true)
                            .setProgramLang(ProgrammerInfo.CodingLanguage.JAVA)
                            .build();


No comments:

Post a Comment