Gazebo model path issue when starting from Java - java

My team is currently working on a project in Java that involves the robot simulation software Gazebo. To launch Gazebo with a specific world, we have written a shell script that we want to execute with the Runtime.getRuntime().exec(...) command in Java (or ProcessBuilder).
Here is our problem:
If we start that script from the terminal, everything works perfectly i.e. you can see a world with one of our models (pylons):
However, if we try to execute that script from within our Java application, it just shows this (models are recognized but not visualized):
We assume that Gazebo doesn't find the gazebo model path although they are defined in ~/.bashrc.
Has anyone an idea why it is not working. We know that most of you may not know Gazebo, but maybe some of you have dealt with similar issues.
Thanks in advance!

I found a solution that works for us:
Instead of the "Runtime.getRuntime().exec(...)" command, I use the ProcessBuilder.
ProcessBuilder builder = new ProcessBuilder();
builder.command(your_command);
builder.directory(new File(your_path));
Map<String, String> env = builder.environment();
env.put("GAZEBO_MODEL_PATH", variable_content);
Process process = builder.start();
Now, we can set GAZEBO_MODEL_PATH here and Gazebo loads all worlds correctly

Related

Netbeans project still running when using `Runtime.getRuntime().exec("explorer.exe");`

I'm working on a (non-malicious) screen-locking sort of Swing application, and I've adapted code from Martijn Courteaux's answer at Use Java to lock a screen to do this. The problem is that when I use Runtime.getRuntime().exec("explorer.exe"); to reopen the Explorer process at program closing, Netbeans thinks that my project is still running because the resulting explorer.exe is running. CMD prompt and JCreator don't have this issue.
Can anyone give an example of the preferred way to call a command like explorer.exe to avoid this happening with Netbeans?
Edit: I close the Explorer process at the start of the program (which includes the taskbar). When I run Explorer, it's not to open a Windows Explorer window (which works totally fine with the given answers) but to restore the regular Windows UI.
The problem is Runtime#exec is waiting for the child process to exit. This is the default behavior.
If you want to execute a parentless process (a process in which the parent process can terminate even though the child is still running), you need to get a little more creative.
We use...
"cmd /C start /B /NORMAL " + yourCommand
I would highlight recommend using ProcessBuilder as it makes it significantly easier to build and execute external commands.
Something like...
ProcessBuilder pb = new ProcessBuilder("cmd", "/C", "start", "/B", "/NORMAL", "explorer.exe");
pb.start();
For example....
nb I run this is NetBeans and the program exited after/as explorer opened.
There is one little draw back. This doesn't like long file names. You'll need to find some way to produce short file names/paths for this to work. I was forced to use JNI solution for this
ProcessBuilder allows you to create and control processes.
Here's some example code, it's fairly complex but I don't have time to dumb it down (no offense): https://github.com/Xabster/Botster/blob/master/src/commands/ExecCommand.java

javac in ProccessBuilder doesn't work in .jar?

My problem is, when I build and run the application using its .jar(distribution ready), this code doesn't work.
filepath = classpath + classname;
ProcessBuilder builder = new ProcessBuilder("javac", filepath + ".java");
builder.redirectErrorStream(true);
process = builder.start();
It works properly when I execute the program using Netbeans. But when it's on its own, it doesn't work.
I'm using ProcessBuilder and Process so that I can get the process' I/O stream later on.
In Netbeans there is a development kit integrated into the environment. Due to this, it will always work with in there. Make sure your environmental variables are set to link to your JDK.
You can try this by going into a cmd.exe window and typing 'javac -version'. If done correctly, it should show the default JDK you have on your system. If it says it can't be found, follow this guide:
http://java.com/en/download/help/path.xml

How to run a script from Java

I have a script, which is test.sh on Ubuntu. I want to run it from Java. I know I have to use Runtime.getRuntime().exec();
Don't I have to fill the exec parenthesis with the location of test.sh? I am typing /home/main/ss/test.sh
I do not get any error messages but when I searched the folder, I saw that script did not work. How can I fix it?
You should really look at Process Builder. It is really built for this kind of thing.
ProcessBuilder pb = new ProcessBuilder("myshellScript.sh", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory(new File("myDir"));
Process p = pb.start();
Did you try to call your script directly, or did you specify a shell to use? You may need to specify the shell on your exec call if it's not specified (and processed) from the first line of the script.
From looking at your description, how you conclude the script didn't work:
I do not get any error messages but when I searched the folder, I saw
that script did not work. How can I fix it?
I conclude, the script creates a new file, and there is none, after starting from Java.
Maybe you assume, that the output of the script will, if sent to the current directory, end in the directory of the script, not in the directory where you started your Java application.
This might be the Desktop, if you start your class from the UI with an Icon as starter, it might be your home or project dir, if you compile the class from hand, or a directory, set up by your IDE, if you develop with eclipse or the like.
If your script writes to an absolute path like /tmp/script.out, you could verify fast, whether this might be your issue.

Powershell process hanging when called from Java application

I am trying to write a simple application that takes in a command line arguement (which will be a Powershell ps1 file) and then run it. So I have experemented with a number of different approaches and seem to be running into a problem. If I attempt to invoke powershell from within java, the windows process is started and is visible via process explorer, however powershell never returns, it hangs in some sort of loop by the looks of it. The command I am using is:
String command = "powershell -noprofile -noninteractive \"&C:\\new\\tst.ps1\"";
The command is then executed using:
Runtime systemRuntime = Runtime.getRuntime();
Process proc = systemRuntime.exec(command);
At the moment I am hard coding the location to the ps1 file as I was trying to rule this out as an issue. Using a process explorer I can see the hanging powershell process and the command that was passed to it was :
powershell -noprofile -noninteractive "&C:\new\tst.ps1"
which when copied into a cmd window, works to launch the tst.ps1 file. The file itself is incredibly simple in this example and I think I can rule it out being the cause of the freeze as I have tried to launch other ps1 files the same behaviour can be seen.
To further add to the confusion, if I use the java code posted above and pass in powershell commands instead of a file name then it successfully runs.
I've scoured the web and see lots of people experiencing the same issue but no one seems to have posted there solution, I hope its a simple oversight on my part and can be easily fixed.
Any hints/tips are appreciated :D
Alan
You have to close OutputStream in order for Powershell to exit.
Runtime systemRuntime = Runtime.getRuntime();
Process proc = systemRuntime.exec(command);
proc.getOutputStream().close();
Is your external program writing to the standard outputs (err and out)?
If yes, it can hang waiting for you to consume them from the java parent process.
You can get those as InputStreams by calling
Process.getInputStream()
and
Process.getErrorStream()
There's more details here:
Javadoc for Process

How i run cygwin using java program?

I have installed cygwin on window to start crawling. It work well.
Now i want to run cygwin and run a crawl program at starting of cygwin using java program code.
If you provide some code for it ,it will be great help for me.
I looked at adatapost's link. It seems like a world of trouble awaits you down this path.
I mean, I like Cygwin a lot, but I wouldn't use it like this.
A few centimetres to the right of the 'Your Answer' box I'm typing in is a link to a Related question 'How can I run cygwin from Java?'
Who's putting the cart before the horse? I don't know.
Does Cygwin have to be involved at all ?
If you are trying to run a binary that requires the cygwin1.dll (which includes most commands you can execute from the cygwin bash shell) then you can run it by specifying the cygwin\bin directory in the path environment variable like this:
Process p = Runtime.getRuntime().exec("C:/path/to/cygwin/binary.exe", new String[] { "PATH=C:\\cygwin\\bin" });
This assumes you installed cygwin in C:\cygwin

Categories