Connecting Tech Pros Worldwide Forums | Help | Site Map

trouble with Runtime.getRuntime().exec(cmd) on Linux

JW
Guest
 
Posts: n/a
#1: Jul 17 '05
Hi,

I don't seem to get any results from the following use
of Runtime.getRuntime().exec(cmd) using Java 1.4 on
Redhat linux.

Suppose that in the same directory as my java file below,
I have files junk1, junk2, junk3, and i want
to have the java program delete all of these files.

However, when I run the following code,
no deletion occurs.
What am I doing wrong?

---
import java.lang.Runtime;
import java.io.*;



public class Commandline
{

String[] cmd = { "rm", "-f","junk*"};
// String[] cmd = { "/bin/sh","rm", "-f","junk*"}; //doesn't work either

public Commandline()
{
try
{
Process pro = Runtime.getRuntime().exec(cmd) ;
}catch (IOException e)
{
System.err.println("ioexception starting process! " + e);
}

}

public static void main (String [] args)
{
Commandline c = new Commandline();
}


}

---

hiwa
Guest
 
Posts: n/a
#2: Jul 17 '05

re: trouble with Runtime.getRuntime().exec(cmd) on Linux


jkpangtang@yahoo.com (JW) wrote in message news:<3607e8e4.0312261746.7fc43aa5@posting.google. com>...[color=blue]
> Hi,
>
> I don't seem to get any results from the following use
> of Runtime.getRuntime().exec(cmd) using Java 1.4 on
> Redhat linux.
>
> Suppose that in the same directory as my java file below,
> I have files junk1, junk2, junk3, and i want
> to have the java program delete all of these files.
>
> However, when I run the following code,
> no deletion occurs.
> What am I doing wrong?
>
> ---
> import java.lang.Runtime;
> import java.io.*;
>
>
>
> public class Commandline
> {
>
> String[] cmd = { "rm", "-f","junk*"};
> // String[] cmd = { "/bin/sh","rm", "-f","junk*"}; //doesn't work either
>
> public Commandline()
> {
> try
> {
> Process pro = Runtime.getRuntime().exec(cmd) ;
> }catch (IOException e)
> {
> System.err.println("ioexception starting process! " + e);
> }
>
> }
>
> public static void main (String [] args)
> {
> Commandline c = new Commandline();
> }
>
>
> }
>
> ---[/color]

'rm' is not a standalone executable in its own right. Use -c option
for the shell. Or use File#delete() method. You must set right
permission for the files.
Scott Lurndal
Guest
 
Posts: n/a
#3: Jul 17 '05

re: trouble with Runtime.getRuntime().exec(cmd) on Linux


jkpangtang@yahoo.com (JW) writes:[color=blue]
>Hi,
>
>I don't seem to get any results from the following use
>of Runtime.getRuntime().exec(cmd) using Java 1.4 on
>Redhat linux.
>
>Suppose that in the same directory as my java file below,
>I have files junk1, junk2, junk3, and i want
>to have the java program delete all of these files.
>
>However, when I run the following code,
>no deletion occurs.
>What am I doing wrong?[/color]

It is 'exec', not 'execvp'. You need to provide the
full path to the executable:

String[] cmd = { "/bin/rm", "-f", "junk*" };

scott

JW
Guest
 
Posts: n/a
#4: Jul 17 '05

re: trouble with Runtime.getRuntime().exec(cmd) on Linux


Hi again,

I tried:
String[] cmd = { "/bin/rm", "-f", "junk*" };

with:
Runtime.getRuntime().exec(cmd) ;

and still no luck.

I even tried sticking the command "rm -f junk*" in
an executable file "script.scr" and calling
the java file containg the lines:
Runtime.getRuntime().exec(cmd)
where
String cmd ="./script.scr".

Nothing happened as well.

I use "rm" as an example.
I may need to call other applications, such as "/sbin/shutdown".

Thanks again in advance.





scott@slp53.sl.home (Scott Lurndal) wrote in message news:<oZkHb.2779$Qh5.2570@newssvr25.news.prodigy.c om>...[color=blue]
> jkpangtang@yahoo.com (JW) writes:[color=green]
> >Hi,
> >
> >I don't seem to get any results from the following use
> >of Runtime.getRuntime().exec(cmd) using Java 1.4 on
> >Redhat linux.
> >
> >Suppose that in the same directory as my java file below,
> >I have files junk1, junk2, junk3, and i want
> >to have the java program delete all of these files.
> >
> >However, when I run the following code,
> >no deletion occurs.
> >What am I doing wrong?[/color]
>
> It is 'exec', not 'execvp'. You need to provide the
> full path to the executable:
>
> String[] cmd = { "/bin/rm", "-f", "junk*" };
>
> scott[/color]
Raymond DeCampo
Guest
 
Posts: n/a
#5: Jul 17 '05

re: trouble with Runtime.getRuntime().exec(cmd) on Linux


JW wrote:[color=blue]
> Hi again,
>
> I tried:
> String[] cmd = { "/bin/rm", "-f", "junk*" };
>
> with:
> Runtime.getRuntime().exec(cmd) ;
>
> and still no luck.
>
> I even tried sticking the command "rm -f junk*" in
> an executable file "script.scr" and calling
> the java file containg the lines:
> Runtime.getRuntime().exec(cmd)
> where
> String cmd ="./script.scr".
>
> Nothing happened as well.
>
> I use "rm" as an example.
> I may need to call other applications, such as "/sbin/shutdown".
>
> Thanks again in advance.
>[/color]

It doesn't sound as if you are very familiar with the Unix environment,
so I am going to take that approach.

When you enter commands at a command prompt in Unix, you are really
sending the commands to a shell program. There are a few different
shell programs in Unix; the most popular are the Bourne shell (/bin/sh),
the Korn shell (/bin/ksh), the Bash shell (/bin/bash), the C shell
(/bin/tcsh) and the extended C shell (/bin/tcsh - the name escapes me at
the moment). The shell interprets the commands and executes processes
for you. You can see what shell you are running under by typing "echo
$SHELL".

The shell provides some built in commands. For example, cp is usually a
built in command. It is important to know when a command is a built in
command vs a stand-alone executable when executing them from another
program.

The simplest way for you to get what you want is to always execute
commands via the shell. You can do this by calling the shell with the
"-c" option in most cases. E.g.

Runtime.getRuntime().exec("/bin/bash -c 'rm -f junk'");

If you would like to put your commands in a script, place the following
line at the beginning of the script:

#!/bin/bash

Note that there must not be any whitespace at the beginning of the line.
Then give your script executable permissions:

chmod a+x script.scr

Now you can treat the script just like an executable file:

Runtime.getRuntime().exec("./script.scr");

HTH,
Ray



Carl
Guest
 
Posts: n/a
#6: Jul 17 '05

re: trouble with Runtime.getRuntime().exec(cmd) on Linux




JW wrote:[color=blue]
> Hi,
>
> I don't seem to get any results from the following use
> of Runtime.getRuntime().exec(cmd) using Java 1.4 on
> Redhat linux.
>[/color]

why dont you grab the input stream from the runtime you are using and
print that to your console. that way you can see the OS response to
your failed command.

you can look at the source code on www.rigidsoftware.com, chessone
executes a program on the command line in linux. its been a while so I
dont remember how i did it, but its in the source code. probably unzip
the source and do a search on each file for the one containing the
getRuntime command...


CL

Closed Thread