Quick Start
CONTENT

Usage

As with any Java module you can use jrosclient in any of the following ways.

With Maven or Gradle

See starter project as an example.

Installing manually

If you like you can download and install jrosclient manually.

Download jrosclient package and unzip it to the folder of your choice:

unzip -d <JROSCLIENT_INSTALL_DIR> jrosclient.zip

Inside you will find:

jrosclient has a modular nature so you can use it as:

Samples

Publishing to the topic

Let`s write an application which will create a topic called helloRos and will publish a new message there every second.

Application code PublisherApp.java:

import id.jrosclient.JRosClient;
import id.jrosclient.TopicSubmissionPublisher;
import id.jrosmessages.std_msgs.StringMessage;

/**
 * Creates a new topic and publishes messages to it.
 */
public class PublisherApp {

    public static void main(String[] args) throws Exception {
        // specify URL of the master node
        var client = new JRosClient("http://localhost:11311/");
        String topicName = "/helloRos";
        var publisher = new TopicSubmissionPublisher<>(StringMessage.class, topicName);
        // register a new publisher for a new topic with ROS
        client.publish(publisher);
        while (true) {
            publisher.submit(new StringMessage().withData("Hello ROS"));
            System.out.println("Published");
            Thread.sleep(1000);
        }

        // usually we need to close client once we are done
        // but here we keep it open so that subscriber will keep
        // printing messages indefinitely
    }
}

Now start:

java -cp "<JROSCLIENT_INSTALL_DIR>/libs/*" PublisherApp.java
Published
Published
Published
Published
...

Now in other terminal lets use rostopic command to subscribe to helloRos topic and see the messages:

rostopic echo helloRos
data: "Hello ROS"
---
data: "Hello ROS"
---
data: "Hello ROS"
---
data: "Hello ROS"
---
data: "Hello ROS"

Subscribing to the topic

Having publisher running from previous example we can use jrosclient to subscribe to helloRos topic to receive all messages from it.

Application code SubscriberApp.java:

import id.jrosclient.JRosClient;
import id.jrosclient.TopicSubscriber;
import id.jrosmessages.std_msgs.StringMessage;

/**
 * Demonstrates how to subscribe to ROS topics.
 */
public class SubscriberApp {

    public static void main(String[] args) throws Exception {
        // specify URL of the master node
        var client = new JRosClient("http://localhost:11311/");
        var topicName = "/helloRos";
        // register a new subscriber
        client.subscribe(new TopicSubscriber<>(StringMessage.class, topicName) {
            @Override
            public void onNext(StringMessage item) {
                System.out.println(item);
                // request next message
                getSubscription().request(1);
            }
        });

        // usually we need to close client once we are done
        // but here we keep it open so that subscriber will keep
        // printing messages indefinitely
    }
}

To run:

java -cp "<JROSCLIENT_INSTALL_DIR>/libs/*" SubscriberApp.java
{ "data": "Hello ROS" }
{ "data": "Hello ROS" }
{ "data": "Hello ROS" }

More samples

More Java samples including those provided above can be found in <JROSCLIENT_INSTALL_DIR>/samples folder or in Git repository.

To run samples use:

java -cp "<JROSCLIENT_INSTALL_DIR>/libs/*" <SAMPLE_PATH>

For example here is how you can run PublisherApp sample from the jrosclient installation folder:

java -cp "libs/*" samples/PublisherApp.java
Published
Published
Published
...

Logging

To keep 3rd party dependencies of jrosclient to minimum it uses java.util.logging (JUL) for logging. Many logging libraries provide JUL adapters so you can still use them to manage its logging.

jrosclient comes with two logging configurations (info, debug). You can enable them by specifying "java.util.logging.config.file" property:

java -Djava.util.logging.config.file=jrosclient-debug.properties -cp "libs/*" samples/PublisherApp.java