As with any Java module you can use jrosclient in any of the following ways.
See starter project as an example.
If you like you can download and install jrosclient manually.
Download jrosclient package and unzip it to the folder of your choice:
Inside you will find:
jrosclient has a modular nature so you can use it as:
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:
Published Published Published Published ...
Now in other terminal lets use rostopic command to subscribe to helloRos topic and see the messages:
data: "Hello ROS" --- data: "Hello ROS" --- data: "Hello ROS" --- data: "Hello ROS" --- data: "Hello ROS"
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:
{ "data": "Hello ROS" } { "data": "Hello ROS" } { "data": "Hello ROS" }
More Java samples including those provided above can be found in <JROSCLIENT_INSTALL_DIR>/samples folder or in Git repository.
To run samples use:
For example here is how you can run PublisherApp sample from the jrosclient installation folder:
Published Published Published ...
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: