Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Running multiple Unix commands in Java

Reply
Thread Tools

Running multiple Unix commands in Java

 
 
Jerome
Guest
Posts: n/a
 
      01-24-2005
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.


 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      01-24-2005
Jerome wrote:
> 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

--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      01-24-2005
On Mon, 24 Jan 2005 11:41:51 -0000, Jerome wrote:
> 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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Juha Laiho
Guest
Posts: n/a
 
      01-24-2005
"Jerome" <(E-Mail Removed)> 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.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing Multiple Unix Commands in one SSH Session from Java Program gravi2020 Java 0 01-15-2009 04:36 PM
Running DOS Commands with Unix UNC Paths within ASP Anonymous ASP General 0 04-03-2008 06:04 AM
Problem running multiple lwp-download commands from a Windows batch file Bruce Horrocks Perl Misc 3 01-01-2004 01:31 AM
Run unix commands from Java Yadagiri Rao KP Java 2 08-05-2003 02:11 PM
Re: man pages for C commands (GCC commands) Ben Pfaff C Programming 4 06-28-2003 06:21 PM



Advertisments