Rosanswers logo


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: http://rosjava.github.io/rosjava_core/latest/getting_started.html

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 setup.sh 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 : http://wiki.ros.org/ROS/Tutorials/CreatingMsgAndSrv)

~/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 : http://wiki.ros.org/rosjava/Tutorials/hydro/Unofficial%20Messages)
~/catkin_ws/src/myjava_pkgs/pub_sub (using: catkin_create_rosjava_project pub_sub, as given at: http://wiki.ros.org/rosjava_build_tools/Tutorials/hydro/Creating%20Rosjava%20Packages
This automatically created : ~/catkin_ws/src/myjava_pkgs/pub_sub/src/main/java/com/github/rosjava/pub_sub/Dude.java)

Then I created Talker.java and Listener.java in:-


as given at: http://rosjava.github.io/rosjava_core/latest/getting_started.html

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 http://wiki.ros.org/rosjava_core/graveyard/rosjava_tutorial_pubsub 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 'http://robotbrains.hideho.org/nexus/content/groups/ros-public'

// 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:-

    import foo_msgs.foo;

    I get compilation error as:-

    /home/aknirala/catkin_ws/src/myjava_pkgs/pub_sub/src/main/java/org/ros/rosjava_tutorial_pubsub/Talker.java:10: package foo_msgs does not exist
    import foo_msgs.foo;

    Should I include foo_msgs somewhere else as well, to make it available to Talker?

  2. I am able to compile and run service as well using command:
    ./build/install/pub_sub/bin/pub_sub org.ros.rosjava_tutorial_services.Server
    I can test it by running client using:
    ./build/install/pub_sub/bin/pub_sub org.ros.rosjava_tutorial_services.Client
    which returns correct result. But when I try to invoke it from command line using
    rosservice call /add_two_ints 3 7
    I get an error:-
    ERROR: Unable to load type [rosjava_test_msgs/AddTwoInts].
    Have you typed 'make' in [rosjava_test_msgs]?
    How to fix it? (All the four combination of Talker and Listener from java and C++ side works with each other, but I can't use the java server from command line, I can use the C++ version though)

    It is not using the srv file in foo_msgs but is using rosjava_test_msgs from jar : /opt/ros/hydro/share/maven/org/ros/rosjava_messages/rosjava_test_msgs/0.1.27/rosjava_test_msgs-0.1.27.jar

    How can I use my custom service over here?

  3. What all commands are executed when I run shortcut commands related to rosjava like:
    How can I customize it?

Any help/comment/pointers is greatly appreciated.

Update: This is how I am writing my Talker.java (its onStart function) for using my custom message foo

public class Talker extends AbstractNodeMain {

  public void onStart(final ConnectedNode connectedNode) {
    final Publisher//
                    //Changed type from std_msgs.String to foo_msgs.foo
    publisher =
        connectedNode.newPublisher("chatter", foo_msgs.foo._TYPE); //changed to foo over here
    // This CancellableLoop will be canceled automatically when the node shuts
    // down.
    connectedNode.executeCancellableLoop(new CancellableLoop() {
      private int sequenceNumber;

      protected void setup() {
        sequenceNumber = 0;

      protected void loop() throws InterruptedException {
        foo_msgs.foo str = publisher.newMessage();  //Creating object of foo
        str.setData("Hello world! " + sequenceNumber);  //THIS PART IS NOT ALLOWED

The line : str.setData("Hello world! " + sequenceNumber); is not allowed as str.setData is expecting std_msgs.String and nt java String. How to fix this?

Originally posted by aknirala on ROS Answers with karma: 339 on 2013-12-28

Post score: 0


1 Answer 1


Rosanswers logo

  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.

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

  2. These scripts are driven by the code and templates at https://github.com/rosjava/rosjava_build_tools/tree/hydro/src/rosjava_build_tools. 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!

Originally posted by Daniel Stonier with karma: 3170 on 2014-01-01

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by aknirala on 2014-01-02:
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 Talker.java. When I try to set a string to object of foo, I get a type cast error.

I have updated d Q.

Comment by Daniel Stonier on 2014-01-13:
Provide it a std_msgs/String object instead of a java string?

Comment by Rabe on 2014-05-14:
How would I do this?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.