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 answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Writing a Simple Publisher and Subscriber (Java)

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

Keywords: rosjava

Tutorial Level: BEGINNER

Next Tutorial: Building RosJava Libraries

This tutorial describes how to create, compile, and execute a simple publisher and subscriber in rosjava. It assumes that you have installed the deb package for rosjava, as described in rosjava 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).

> source devel/setup.bash
> cd src/rosjava_catkin_package_a
> catkin_create_rosjava_project my_pub_sub_tutorial
> cd ../..
> catkin_make

Executing

> 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_catkin_package_a.my_pub_sub_tutorial.Talker &
> ./my_pub_sub_tutorial com.github.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

Dependencies

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.1,)'
}

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/hydro/share/maven/org/ros/rosjava_core with version between 0.1 and 0.2.

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/hydro/WritingPublisherSubscriber(Java) (last edited 2014-02-12 19:42:46 by bradknox)