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.
- 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
- Extract this zip file into a directory and add the bin directory under this into the system path environment variable.
- Checkout the source code from Github repository - As v3.3.0 is the latest release, checkout the same tag rather than master.
- Copy protoc.exe from step 1 and paste it under src directory in the checked out location.
- Continue to run maven tasks as provided here - https://github.com/google/protobuf/tree/master/java
To run the examples in the library -
- 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.
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.
Once the generated code is in the example classpath, the code can be executed.
As a simpler approach, especially for occasional users, you could also just:
- copy and paste your schema fragment here: https://protogen.marcgravell.com
- select "(protoc) Java" (or whatever language you need), and click "generate"
- 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