Ask Your Question

rosjava - errors setting up a simple publisher

asked 2013-11-14 09:56:52 -0600

Albatross gravatar image

updated 2013-11-15 10:34:48 -0600

I'm trying to set up a simple publisher using rosjava (pure java, I don't need Android). I've been following some tutorials and making lots of progress, but now I'm stuck. Here is the code I have so far:

import org.ros.concurrent.CancellableLoop;
import org.ros.namespace.GraphName;
import org.ros.node.AbstractNodeMain;
import org.ros.node.ConnectedNode;
import org.ros.node.NodeMain;
import org.ros.node.topic.Publisher;

import org.ros.node.NodeConfiguration;
import org.ros.node.Node;
import org.ros.internal.node.DefaultNode;

import org.ros.node.DefaultNodeFactory;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.Executors;

public class Publisher_Test {

  private DefaultNode node;
  //private Publisher<std_msgs.String> publisher;

  private final ScheduledExecutorService scheduler =

  public Publisher_Test() {
    NodeConfiguration node_cfg = NodeConfiguration.newPrivate();
    DefaultNodeFactory factory = new DefaultNodeFactory( scheduler );
    node = (DefaultNode)factory.newNode( node_cfg );

    System.out.println("In Constructor");
    final Publisher<std_msgs.String> publisher = node.newPublisher("chatter", std_msgs.String._TYPE);
    node.executeCancellableLoop( new CancellableLoop() {

            private int seq;

        protected void setup() {
            System.out.println("Setting up loop");

        protected void loop() throws InterruptedException {

            try {
              while (true) {
            std_msgs.String str = publisher.newMessage();
            str.setData( "Hello world! " + seq++ );
            } catch (Exception e) {

I've been basing this off of this question and this tutorial. Some of the syntax used is out of data, so I've been looking through the source code here to get things to compile.

I'm using ROS Hydro on an Ubuntu 13.04 machine. My goal is to get a publisher and subscriber implemented in pure java running in jython. I've compiled my class using javac, and can import it into jython fine, I get an error when I try to set up my node in the constructor for Publisher_Test:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    at org.ros.concurrent.ListenerGroup.addAll(
    at org.ros.concurrent.ListenerGroup.addAll(
    at org.ros.internal.node.DefaultNode.<init>(
    at org.ros.node.DefaultNodeFactory.newNode(
    at org.ros.node.DefaultNodeFactory.newNode(
    at Publisher_Test.<init>(
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
    at java.lang.reflect.Constructor.newInstance(
    at org.python.core.PyReflectedConstructor.constructProxy(

java.lang.NullPointerException: java.lang.NullPointerException

From what I understand so far, it looks like DefaultNodeFactory is setting up the DefaultNode with the Collection<nodelistener> to be null (which looking at the source code seems to be a valid thing to do), and then the addAll() method which is called in the DefaultNode constructor throws this exception when it tries to iterate on null.

I'm thinking I need to give a Listener to the DefaultNodeFactory, but I am unsure how I would go about doing this. What is a ... (more)

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted

answered 2013-11-26 13:08:45 -0600

Albatross gravatar image

After many hours of trying various things, I've finally managed to get something that works! The key was writing my own version of RosRun for my application, and then creating an instance of the custom RosRun in the java application that I want to use ROS from.

The code looks something like this:


import org.ros.exception.RosRuntimeException;
import org.ros.internal.loader.CommandLineLoader;
import org.ros.node.DefaultNodeMainExecutor;
import org.ros.node.NodeConfiguration;
import org.ros.node.NodeMain;
import org.ros.node.NodeMainExecutor;

// This class will run a publisher and subscriber, and relay data between them.

public class Run_PubSub {

  private Talker pubNodeMain;
  private Listener subNodeMain;

  public Run_PubSub() {
    // Set up the executor for both of the nodes
    NodeMainExecutor nodeMainExecutor = DefaultNodeMainExecutor.newDefault();

    // Load the publisher
    String[] pubArgv = { "Talker" };
    CommandLineLoader pubLoader = new CommandLineLoader(Lists.newArrayList(pubArgv));
    String nodeClassName = pubLoader.getNodeClassName();
    System.out.println("Loading node class: " + pubLoader.getNodeClassName());
    NodeConfiguration pubNodeConfiguration =;

    try {
      pubNodeMain = (Talker)pubLoader.loadClass(nodeClassName);
    } catch (ClassNotFoundException e) {
      throw new RosRuntimeException("Unable to locate node: " + nodeClassName, e);
    } catch (InstantiationException e) {
      throw new RosRuntimeException("Unable to instantiate node: " + nodeClassName, e);
    } catch (IllegalAccessException e) {
      throw new RosRuntimeException("Unable to instantiate node: " + nodeClassName, e);

    Preconditions.checkState(pubNodeMain != null);
    nodeMainExecutor.execute(pubNodeMain, pubNodeConfiguration);

    // Load the subscriber
    String[] subArgv = { "Listener" };
    CommandLineLoader subLoader = new CommandLineLoader(Lists.newArrayList(subArgv));
    nodeClassName = subLoader.getNodeClassName();
    System.out.println("Loading node class: " + subLoader.getNodeClassName());
    NodeConfiguration subNodeConfiguration =;

    try {
      subNodeMain = (Listener)subLoader.loadClass(nodeClassName);
    } catch (ClassNotFoundException e) {
      throw new RosRuntimeException("Unable to locate node: " + nodeClassName, e);
    } catch (InstantiationException e) {
      throw new RosRuntimeException("Unable to instantiate node: " + nodeClassName, e);
    } catch (IllegalAccessException e) {
      throw new RosRuntimeException("Unable to instantiate node: " + nodeClassName, e);

    Preconditions.checkState(subNodeMain != null);
    nodeMainExecutor.execute(subNodeMain, subNodeConfiguration);

  public void do_stuff() {

This can be compiled with javac, and you don't need to use gradle or catkin (as long as you set up your classpath properly). I found this super easy to integrate into my other projects, and hopefully it is helpful to others too. I based it off of this code

edit flag offensive delete link more


I actually have to publish from rospy and subscribe to that topic with rosjava. For the build tutorial u posted, I tried executing the command ./gradlew install but there was an error staing command not found. WHat do i do?

uzair gravatar imageuzair ( 2013-11-26 15:58:05 -0600 )edit

There should be a file called gradlew in that folder. Some versions of the code don't have it (the first one I tried didn't). I think if you use apt-get it won't be there. Try: git clone git checkout -b hydro origin/hydro hopefully that version will have it

Albatross gravatar imageAlbatross ( 2013-11-26 17:12:52 -0600 )edit

I can't seem to get the formatting working right in the comments, but there are two commands there to install from source, that I got from this page:

Albatross gravatar imageAlbatross ( 2013-11-26 17:14:25 -0600 )edit

Thanks a lot for this code example it helps me a lot integrating some Ros functionalities into my project under eclipse. But I still have a small question: Where do you find the libraries needed for rosjava?

Mehdi. gravatar imageMehdi. ( 2014-03-23 21:19:27 -0600 )edit

For me I didn't use eclipse, and I had to manually find and download various libraries to get it running. Most things were installed in the /opt/ros/hydro/share/maven/ directory, I kept a list of the other dependencies that I will post below.

Albatross gravatar imageAlbatross ( 2014-03-25 09:17:46 -0600 )edit

Hey thanks for your help, I meanwhile was able to make it work finally! If some people have problems with 'node Talker not found' or something like that, like I did and want to avoid headaches, just delete the code inside try {} and replace the line : private Talker pubNodeMain; by private Talker pubNodeMain = new Talker(); same for listener

Mehdi. gravatar imageMehdi. ( 2014-03-25 15:20:18 -0600 )edit

answered 2014-04-12 23:35:48 -0600

pico gravatar image

Thanks for this example; i have tried to run your main but i have this error:

Apr 13, 2014 11:33:58 AM org.ros.internal.node.client.Registrar INFO: MasterXmlRpcEndpoint URI: http://localhost:11311 Apr 13, 2014 11:33:58 AM org.ros.internal.node.client.Registrar onPublisherAdded INFO: Registering publisher: Publisher, TopicIdentifier>, Topic, TopicDescription>>> Apr 13, 2014 11:33:58 AM org.ros.internal.node.client.Registrar callMaster INFO: Response, TopicIdentifier>, Topic, TopicDescription>>> Apr 13, 2014 11:33:58 AM org.ros.internal.node.client.Registrar onPublisherAdded INFO: Registering publisher: Publisher, TopicIdentifier>, Topic, TopicDescription>>> Apr 13, 2014 11:33:58 AM org.ros.internal.node.client.Registrar callMaster INFO: Response, TopicIdentifier>, Topic, TopicDescription>>> Apr 13, 2014 11:33:58 AM org.apache.xmlrpc.server.XmlRpcStreamServer execute

SEVERE: execute: Error while performing request org.apache.xmlrpc.server.XmlRpcNoSuchHandlerException: No such handler: requestTopic at org.apache.xmlrpc.server.AbstractReflectiveHandlerMapping.getHandler( at org.apache.xmlrpc.server.XmlRpcServerWorker.execute( at org.apache.xmlrpc.server.XmlRpcServer.execute( at org.apache.xmlrpc.server.XmlRpcStreamServer.execute( at at org.apache.xmlrpc.util.ThreadPool$MyThread.runTask( at org.apache.xmlrpc.util.ThreadPool$

Do you have any idea whi I have this error?


edit flag offensive delete link more


This is not a forum, and your "answer" is not an answer to the original question. Please open up a new question instead. You can include a link to this one, if you like.

Martin Günther gravatar imageMartin Günther ( 2014-04-13 23:09:49 -0600 )edit

answered 2013-11-14 22:23:05 -0600

Forget about the first link, it's completely outdated. The second link is almost up to date, but it has been updated and moved here. If all you want is a publisher, you shouldn't have to modify that code at all. Also have a look at rosjava_tutorial_pubsub for the latest code.

edit flag offensive delete link more


Thanks for the updated link! It is still not clear to me how I would run the code. I haven't been able to get the tutorial to work, and ideally I'd want to be setting things up a little differently for my use case. I'd prefer not to have to use packages and workspaces. I'll update my question

Albatross gravatar imageAlbatross ( 2013-11-15 04:43:59 -0600 )edit

I am also facing similar problems. I cant get it to compile. How do I add the dependencies for the package?

uzair gravatar imageuzair ( 2013-11-15 22:30:05 -0600 )edit

I don't have experience with rosjava on hydro, but some more tutorials are here: . Looks like you could follow the steps under "Creating Rosjava Packages" and add the dependencies to the generated package.xml.

Martin Günther gravatar imageMartin Günther ( 2013-11-15 22:54:32 -0600 )edit

As martin mentioned, start using the github rosjava repositories, in particular the stable hydro branches and follow the instructions at Thats the current development sweet spot and where people will be able to help/bugfix for you.

Daniel Stonier gravatar imageDaniel Stonier ( 2013-11-20 17:44:18 -0600 )edit

I am following the tutorials u mentioned Daniel but there is none for a publisher and subscriber in this set of tutorials. That is why I had to go to the older tutorial which is the snapshot documentation. All I am trying to do is compile a subscriber code with catkin_make. I have been stuck for weeks now on this.

uzair gravatar imageuzair ( 2013-11-26 12:44:17 -0600 )edit

Recently I've been able to get the [tutorial]( that Martin mentioned to work, by first following the [build]( tutorial. I have ros hydro, but I didn't use catkin.

Albatross gravatar imageAlbatross ( 2013-11-26 12:55:01 -0600 )edit

I'd also like to point out that there was an error in the "Executing Nodes" section of the tutorial. The path for ./build/install/rosjava_tutorial_pubsub/bin/rosjava_tutorial_pubsub was slightly different on my system, I think there was no build directory, just install. Something to watch out for.

Albatross gravatar imageAlbatross ( 2013-11-26 12:58:09 -0600 )edit

Ah, I see the problem. It's too roundabout to get to the api docs so I updated that wiki page with information. I also picked up on the errors in the executing nodes section and they got commited last weekend already. Thanks for the feedback.

Daniel Stonier gravatar imageDaniel Stonier ( 2013-11-26 19:39:14 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2013-11-14 09:56:52 -0600

Seen: 2,469 times

Last updated: Apr 12 '14