Sending and receiving LCM messages with Java
This tutorial will show you how to use all of the core functionality of LCM, and is intended for those who have a working knowledge of Java. For detailed information about LCM, please see the Java API reference.
The topics covered in this tutorial are:
This tutorial uses the
example_t message type defined in the type definition tutorial, and assumes that you have generated the Java bindings for the example type by running
After running this command, you should have one file,
exlcm/example_t.java. This file is the Java binding for the example message type. If you have the time, take a moment to open up the file and inspect the generated code.
You can then compile this into a .class file, and then create a .jar archive. Assuming that
lcm.jar is in the current directory (see the Java notes page), you could then run (also from a command shell):
You should then have a file
my_types.jar, which is a Java archive containing the Java bindings for the example message. In order to use LCM types, you must include the jar file on your classpath.
You will need to make sure that lcm.jar is in your classpath, and your Java classes will need to include "import lcm.lcm.*".
To initialize LCM, with default options, simply call:
The default options are suitable for communicating with other LCM applications on the local computer. For communication across computers, or other usages such as reading data from an LCM logfile (e.g., to post-process or analyze previously collected data), see the Java API reference.
We can instantiate and then publish some sample data as follows:
After initializing LCM, this application creates an instance of the example message, fills in some message fields, and then publishes the message.
The call to
lcm.publish() serializes the data into a byte stream and transmits the packet using LCM to any interested receivers. The string
"EXAMPLE" is the channel name, which is a string transmitted with each packet that identifies the contents to receivers. Receivers subscribe to different channels using this identifier, allowing uninteresting data to be discarded quickly and efficiently.
In order to receive messages, you must implement an LCMSubscriber and pass it to LCM.subscribe(). The subscriber will be provided with a LCMDataInputStream that can be read for the message contents. All LCM data types include a constructor that takes a DataInput (including instances of LCMDataInputStream) as an argument. First, let's look at the subscriber:
Next, we can subscribe to the message with:
The LCM instance has a background thread that constantly listens for messages. When a message on channel
"EXAMPLE" arrives, the LCM thread will invoke the messageReceived() method.
Here's an example of a complete subscriber application:
The full example is included in the LCM source distribution, in the
To compile and run the examples, let's assume that the
lcm.jar file is in the current directory, along with
example_t.lcm. We can run our programs by executing the commands:
# 1. Create the Java implementation of temperature_t.lcm lcm-gen -j example_t.lcm # 2. Compile the demo applications and the LCM type created above. javac -cp .:lcm.jar *.java exlcm/*.java # 3. Run MySubscriber (in one terminal) java -cp .:lcm.jar MySubscriber # 4. Run SendMessage (in another terminal) java -cp .:lcm.jar SendMessage
See the Java notes page for some additional information related to LCM development with Java.