Note: This tutorial assumes that you have completed the previous tutorials: Installation, Creating Rosjava Packages.
(!) Please ask about problems and questions regarding this tutorial on Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

A RosJava Publisher and Subscriber (Catkin Style)

Description: How to create, compile, and execute a simple publisher and subscriber in rosjava.

Keywords: rosjava

Tutorial Level: BEGINNER

Next Tutorial: Using Rosjava Services

This tutorial describes how to create, compile, and execute a simple publisher and subscriber in rosjava. It assumes that you have a ros-catkin environment from a source or deb installation and have skimmed through the creating rosjava packages tutorial.

Creating and Running Talker-Listener

Creating a Gradle Sub-Project

This assumes you have already initialised a catkin-gradle package for your java projects. If you haven't done so already, refer to the Creating Rosjava Packages tutorial.

A catkin_create wizard is available that automagically creates the files required for talker-listener in java (effectively reproduces rosjava_core's pubsub tutorial in your own workspace).

> cd src/rosjava_catkin_package_a
> catkin_create_rosjava_project my_pub_sub_tutorial
> cd ../..
> catkin_make

Executing using rosrun

Catkin will have generated executable scripts that can run the nodes for you. From the same directory that you used to run catkin_make:

> source devel/setup.bash
> roscore &
> rosrun rosjava_catkin_package_a my_pub_sub_tutorial com.github.rosjava.rosjava_catkin_package_a.my_pub_sub_tutorial.Talker
> # You will need to choose the script under `bin` directory. Then, from another terminal: 
> rosrun rosjava_catkin_package_a my_pub_sub_tutorial com.github.rosjava.rosjava_catkin_package_a.my_pub_sub_tutorial.Listener

Rosrun may warn you about multiple executables; use the one under bin directory.

Executing without rosrun

You can also call the generated scripts manually.

> roscore &
> cd src/rosjava_catkin_package_a/my_pub_sub_tutorial
> cd build/install/my_pub_sub_tutorial/bin
> ./my_pub_sub_tutorial com.github.rosjava.rosjava_catkin_package_a.my_pub_sub_tutorial.Talker &
> ./my_pub_sub_tutorial com.github.rosjava.rosjava_catkin_package_a.my_pub_sub_tutorial.Listener

You should see output similar to this:

Feb 08, 2014 11:11:31 PM org.ros.internal.node.RosoutLogger info
INFO: I heard: "Hello world! 95"
Feb 08, 2014 11:11:32 PM org.ros.internal.node.RosoutLogger info
INFO: I heard: "Hello world! 96"

Under the Hood

Gradle installApp Task

When you run catkin_make, cmake runs through your entire workspace and when it gets to your new project, it will pass off the build to gradle. The targets that gradle builds can be seen listed in your CMakeLists.txt:

catkin_rosjava_setup(publishMavenJavaPublicationToMavenRepository installApp)

In particular, the installApp target. This creates the scripts that we run above and this is what differentiates this gradle subproject as a binary project.

Compiling with Gradle

You could alternatively just compile your subproject alone with gradle (much faster than running catkin_make across your entire workspace):

> source devel/setup.bash
> cd src/rosjava_catkin_package_a/my_pub_sub_tutorial
> ../gradlew installApp


A brief analysis of the tutorial's build.gradle is important:

# src/rosjava_catkin_package_a/my_pub_sub_tutorial/build.gradle

apply plugin: 'application'
mainClassName = 'org.ros.RosRun'

dependencies {
  compile 'org.ros.rosjava_core:rosjava:[0.2,)'

The application plugin is what creates the gradle installApp target, and the rosjava_core dependency instructs gradle to look for a maven artifact to add to the classpath (in this case it will usually be a jar file sitting in /opt/ros/indigo/share/maven/org/ros/rosjava_core with version between 0.2 and 0.3.

Java Package Name

By default, the catkin_create script creates a package name for you along the lines of com.github.<catkin_package_name>.<gradle_project_name>. This will suit you if you are using github for your repositories. If not, refactor your package to suit.

Wiki: rosjava_build_tools/Tutorials/indigo/WritingPublisherSubscriber(Java) (last edited 2017-12-29 15:33:27 by jubeira)