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

including a java byte code class in ros stack

asked 2012-12-11 00:42:20 -0600

safzam gravatar image

Hey all, I have a java program which act like a TCP socket server. And a ros C++ Client node interacts with it and share data on socket. I run my java program using command "java Server" and my C++ ros node like "rosrun my_pkg Cleint". I want to include my java Server program in my ros stack. How can I include and run it there? I dont want to make its node because its not needed at all. I simply want to run it there being included in my stack. Is there any way to run it via roslaunch or how? Also how can I integrate it in my stack? shall I make jar file for Server.class or how? Please give detailed hints, Thanks in advance

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2012-12-11 00:52:28 -0600

updated 2012-12-11 01:01:23 -0600

You can run any program inside a ROS package using rosrun (or equally, using a launch file), no matter if it's a ROS node or not. So I would suggest you put the command(s) you use to run your program from the command line into a shell script and use rosrun to run that script.

Example:

my_pkg/scripts/server.sh:

#!/bin/sh
# TODO: set up CLASSPATH, ...
java $1    # this won't work exactly, you need to add some more 
           # arguments to java (never can remember which)
  1. run from command line:

    $ rosrun my_pkg server.sh $(rospack find my_pkg)/classes/Server.class
    
  2. run from launch file (my_pkg/launch/my_stuff.launch):

    <launch>
      <node pkg="my_pkg" type="server.sh" name="server" args="$(find my_pkg)/classes/Server.class"/>
    </launch>
    

(remember to set the executable bit on server.sh)

edit flag offensive delete link more

Comments

That's actually pretty much what is done in the old JNI based rosjava. rosjava cmake targets generate wrapper scripts that call java with an initial class and make sure that the classpath is set up correctly.

Lorenz gravatar image Lorenz  ( 2012-12-11 02:24:16 -0600 )edit

Hi, It gives me error like ClassNotFoundException: .home.my_pkg.classes.Server.class in both cases in launch and command line. My .sh file works if I simply give java Server instead of java $1. Also if I run java Server form classes folder it works. CLASSPATH is already set in .bashrc. Any hints?

safzam gravatar image safzam  ( 2012-12-12 03:29:56 -0600 )edit

As I wrote, the java call won't work exactly; this was only meant as an example. If it works if you simply write 'java Server', then just do it.

Martin Günther gravatar image Martin Günther  ( 2012-12-12 06:59:20 -0600 )edit

yes I can do that, but the problem is that the Server class also read and write a file in the classes/ directory. Now if I simply add java Server in .sh file and run .sh from other directory then Server program cannot find those files. I want it always to read/write from the same classes/ dir.

safzam gravatar image safzam  ( 2012-12-12 22:42:08 -0600 )edit

It's easy to modify my example above to do just what you want. Just add an echo $1 in the .sh file of my example to see what happens, then make some modifications and add a cd to go to the package directory before running java.

Martin Günther gravatar image Martin Günther  ( 2012-12-13 21:55:03 -0600 )edit

echo $1 and java $1 in .sh with comandline run givs : /home/my_pkg/classes/Server Exception in thread "main" java.lang.NoClassDefFoundError: /home/my_pkg/classes/Server Caused by: java.lang.ClassNotFoundException: .home.my_pkg.classes.Server at java.net.URLClassLoader$1.run(URLClassLoader.java:217)

safzam gravatar image safzam  ( 2012-12-13 22:26:38 -0600 )edit

Question Tools

Stats

Asked: 2012-12-11 00:42:20 -0600

Seen: 278 times

Last updated: Dec 11 '12