Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install and use Protocol Buffers for Java on Windows

The documentation for using Google Protobuf for Java in Windows environment is not very friendly, scattered and puts the new user into confusion. Here is a small attempt to assist the new explorers.

like image 801
Pavan Kumar Avatar asked Jun 05 '17 06:06

Pavan Kumar


2 Answers

  1. Download the Win32 binary from https://github.com/google/protobuf/releases - At the time of writing, the latest is v3.3.0 and hence appropriate binary is https://github.com/google/protobuf/releases/download/v3.3.0/protoc-3.3.0-win32.zip
  2. Extract this zip file into a directory and add the bin directory under this into the system path environment variable.
  3. Checkout the source code from Github repository - As v3.3.0 is the latest release, checkout the same tag rather than master.
  4. Copy protoc.exe from step 1 and paste it under src directory in the checked out location.
  5. Continue to run maven tasks as provided here - https://github.com/google/protobuf/tree/master/java

To run the examples in the library -

  1. Copy the protoc.exe from step 1 above to a preferred location and ensure that location is added into the Path environment variable. To ensure it is done properly, open a new command prompt and run protoc --version to see the appropriate version you installed.
  2. Create the Java package for protocol format given with the examples - defined in addressbook.proto as explained here - protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/addressbook.proto - This generates the required AddressBookProtos class under the package com.example.tutorial which is required for compiling the example code.

  3. Once the generated code is in the example classpath, the code can be executed.

like image 158
Pavan Kumar Avatar answered Nov 09 '22 01:11

Pavan Kumar


As a simpler approach, especially for occasional users, you could also just:

  1. copy and paste your schema fragment here: https://protogen.marcgravell.com
  2. select "(protoc) Java" (or whatever language you need), and click "generate"
  3. copy out your code, or marvel at the in-editor errors and warnings in your schema fragment

Current limitations:

  • only a single input and output document is available - this is purely a UI issue (I need to get a tabbed editor working, hopefully this week)
  • which also means that "import" is heavily constrained - although a few inbuilt imports will work fine ("google/protobuf/duration.proto" for example, will resolve and work correctly)
  • currently, if the "non protoc" parser detects a syntax error and your schema includes the word "import", it won't invoke "protoc" - this is while I investigate whether protoc could ever be abused to access unexpected files
like image 37
Marc Gravell Avatar answered Nov 09 '22 01:11

Marc Gravell