## For instruction on writing tutorials
## http://www.ros.org/wiki/WritingTutorials
####################################
##FILL ME IN
####################################
## for a custom note with links:
## note =
## for the canned note of "This tutorial assumes that you have completed the previous tutorials:" just add the links
## note.0=[[rosjava/Tutorials/indigo/Installation|Installation]]
## note.1=[[rosjava_build_tools/Tutorials/indigo/Creating Rosjava Packages|Creating Rosjava Packages]]
## descriptive title for the tutorial
## title = A RosJava Publisher and Subscriber (Catkin Style)
## multi-line description to be displayed in search
## description = How to create, compile, and execute a simple publisher and subscriber in rosjava.
## the next tutorial description (optional)
## next =
## links to next tutorial (optional)
## next.0.link=[[rosjava_build_tools/Tutorials/indigo/UsingServices|Using Rosjava Services]]
## next.1.link=
## what level user is this tutorial for
## level= BeginnerCategory
## keywords = rosjava
####################################

<<IncludeCSTemplate(TutorialCSHeaderTemplate)>>
<<TableOfContents(4)>>

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 [[rosjava/Tutorials/indigo/Installation|source or deb installation]] and have skimmed through the [[rosjava_build_tools/Tutorials/indigo/Creating Rosjava Packages|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 [[rosjava_build_tools/Tutorials/indigo/Creating Rosjava Packages#RosJava_Catkin_Packages|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 might need to choose the script under `bin` directory if you didn't use the latest rosjava build tools. 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
}}}

=== 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.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.