ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Using custom messages and service in ROSJAVA

asked 2013-12-28 07:53:27 -0600

updated 2014-01-02 05:40:34 -0600


I have been trying to understand ROSJAVA from a while, seems a bit difficult as I couldn't find good tutorials (last week tutorials for Hydro were updated which was v v helpful (many many thanks for that), though they are not sufficient :-( ).

I am using Ubuntu 12.04 and Hydro.

I manage to run ROSJAVA Publishers and subscribers as directed at:

I was able to run Talker from java end and listen it from C++ end, however I am still struggling to find out, how can I use my custom message. Here is my directory structure

~/catkin_ws (catkin workspace, in I am doing source ~/catkin_ws/devel/setup.bash)

~/catkin_ws/src/foo_msgs/msg/foo.msg (contains std_msgs/String data)
~/catkin_ws/src/foo_msgs/srv/AddTwoInts.srv (for checking service, to be checked once I manage to run cutsom messgaes)
~/catkin_ws/src/foo_msgs/CMakeLists.txt  (contains entries to generate message as directed at :

~/catkin_ws/src/myjava_pkgs (ROSJAVA package, created using : catkin_create_rosjava_pkg myjava_pkgs rosjava_bootstrap rosjava_messages foo_msgs)
~/catkin_ws/src/myjava_pkgs/foo_msgs (cretaed by : catkin_create_rosjava_msg_project foo_msgs
as given at :
~/catkin_ws/src/myjava_pkgs/pub_sub (using: catkin_create_rosjava_project pub_sub, as given at:
This automatically created : ~/catkin_ws/src/myjava_pkgs/pub_sub/src/main/java/com/github/rosjava/pub_sub/

Then I created and in:-


as given at:

settings.gradle at ~/catkin_ws/src/myjava_pkgs contains:-

include 'foo_msgs'
include 'pub_sub'

I updated ~/catkin_ws/src/myjava_pkgs/pub_sub/build.gradle seeing The exact content of build.gradle is:-

apply plugin: 'application'

mainClassName = 'org.ros.RosRun'

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'

// uncomment to create an eclipse project using 'gradle eclipse'
//apply plugin: 'eclipse'

sourceCompatibility = 1.6
targetCompatibility = 1.6

// custom maven repository for some rosjava dependencies
repositories {
  maven {
    url ''

// we need this for maven installation
version = '0.0.0-SNAPSHOT'
group = 'ros.my_stack'

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

Now, I am able to compile it from: /catkin_ws/src/myjava_pkgs/pub_sub$ using: ../gradlew installApp

and able to run Talker using: ./build/install/pub_sub/bin/pub_sub org.ros.rosjava_tutorial_pubsub.Talker

My questions are:-

  1. How can I use my custom message, which is in foo.msg?
    On compiling I can see that header files for foo.msg and service are generated at:
    /home/aknirala/catkin_ws/devel/include/foo_msgs and java files are generated at:-
    But how w=can I include it? If I include the line:-
    I get compilation error as:-
    /home/aknirala/catkin_ws/src/myjava_pkgs/pub_sub/src/main/java/org/ros/rosjava_tutorial_pubsub/ package foo_msgs does ...
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-01-01 20:08:50 -0600

Daniel Stonier gravatar image

1) myjava_pkgs/pub_sub is a sibling project of myjava_pkgs/foo. pub_sub itself shouldn't need much of an update after running catkin_create_rosjava_project pub_sub but the key part if you want to use your foo artifact is listing it as a dependency. That appears to be missing in your build.gradle where the only dependency you have listed is rosjava_core: dependencies { compile 'org.ros.rosjava_core:rosjava:[0.1,)' } There are two kinds of dependencies - that one for rosjava_core is an external dependency, but since foo here is a sibling project then it needs to be expressed as an internal dependency, e.g.:

dependencies { compile 'org.ros.rosjava_core:rosjava:[0.1,)' compile project(':foo') }

otherwise foo imports will never get very far.

2) It should never actually try to pull rosjava_test_msgs in any shape or form. Maybe it's something stray in your setup, or something in the build - I'd need to test. Anyway, try and fix 1) and see if you still have a problem here.

3) These scripts are driven by the code and templates at You can play around with them locally by putting a hydro fork of that repo in your source workspace, or just directly editing files in /opt/ros/hydro/lib/python2.7/dist-packages/rosjava_build_tools. Send me a pull request if you make anything useful!

edit flag offensive delete link more


Thnx a lot 4 d sol, now I am able to compile, and able to run server with my custom written service (didn't check client so far), However, I am still not able to use my custom message 'foo' in When I try to set a string to object of foo, I get a type cast error. I have updated d Q.

aknirala gravatar image aknirala  ( 2014-01-02 05:28:19 -0600 )edit

Provide it a std_msgs/String object instead of a java string?

Daniel Stonier gravatar image Daniel Stonier  ( 2014-01-13 13:51:52 -0600 )edit

How would I do this?

Rabe gravatar image Rabe  ( 2014-05-14 05:12:14 -0600 )edit

Question Tools

1 follower


Asked: 2013-12-28 07:53:27 -0600

Seen: 2,186 times

Last updated: Jan 02 '14