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