Running multiple Unix commands in Java

J

Jerome

Hi there,

I am trying to execute multiple unix commands in Java. i.e. cd into the
directory, newline, and enter a command to run something.

--In the terminal window
$ cd xx/xx/xx/xx/
$ runXX

-- The Java
String cmd = "/bash/sh cd/xx/xx/xx '\n' runXX";

Runtime rt1 = Runtime.getRuntime();
Process p1= rt1.exec("cmd");

My java compiles but does not work with the string I have.

Can anyone help.
Thanks in advance.

J.
 
T

Thomas Weidenfeller

Jerome said:
String cmd = "/bash/sh cd/xx/xx/xx '\n' runXX";

The first thing would be to get the Unix command right. The above won't
even run from the command line. And Java will not magically fix your syntax.

"man bash" should give you some information on how to run a command from
a command line argument. Pay special attention to the "-c" option. And
I would bet it is not "/bash/sh" but "/bin/sh" orn "/bin/bash" for a start.

/Thomas
 
G

Gordon Beaton

I am trying to execute multiple unix commands in Java. i.e. cd into
the directory, newline, and enter a command to run something.

In addition to Thomas' comments, consider that there is a version of
Runtime.exec() that already does just that, i.e. lets you specify a
directory to run the command in:

File dir = new File("/home/gordon/slask");
Process p = Runtime.getRuntime().exec("/bin/somecmd", null, dir);

/gordon
 
J

Juha Laiho

Jerome said:
I am trying to execute multiple unix commands in Java. i.e. cd into the
directory, newline, and enter a command to run something.

--In the terminal window
$ cd xx/xx/xx/xx/
$ runXX

-- The Java
String cmd = "/bash/sh cd/xx/xx/xx '\n' runXX";

Runtime rt1 = Runtime.getRuntime();
Process p1= rt1.exec("cmd");

If this really is your code (instead of just a typo), you just tried to run
command named cmd (literally!), which may or may not exist.

As to the syntax of 'String cmd', use semicolons instead newlines to
separate commands.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top