By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,081 Members | 2,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,081 IT Pros & Developers. It's quick & easy.

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

P: n/a
JW
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();
}
}

---
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
jk********@yahoo.com (JW) wrote in message news:<36**************************@posting.google. com>...
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();
}
}

---


'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.
Jul 17 '05 #2

P: n/a
jk********@yahoo.com (JW) writes:
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?


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

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

scott

Jul 17 '05 #3

P: n/a
JW
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.

sc***@slp53.sl.home (Scott Lurndal) wrote in message news:<oZ*****************@newssvr25.news.prodigy.c om>...
jk********@yahoo.com (JW) writes:
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?


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

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

scott

Jul 17 '05 #4

P: n/a
JW wrote:
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.


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

Jul 17 '05 #5

P: n/a


JW wrote:
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.


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

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.