473,506 Members | 17,176 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
5 40440
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
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
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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9011
by: aa | last post by:
Anybody know the solution to trailing space in argument of Runtime.getRuntime().exec(arg), Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler C:\\Documents and...
1
5058
by: Hal Vaughan | last post by:
I've been using Runtime.exec() like this: Runtime rt = Runtime.getRuntime(); try {Process p = rt.exec("MyCommand.bat");} catch (Exception e) {do stuff} When I start my Java classes, I start...
1
2271
by: C. Allen Sher | last post by:
Has anyone ran the Unix "newgrp" command from a java Runtime object? I can't seem to get it to work the way it should.
5
14387
by: Ayesha Ahsan | last post by:
Hi, I use Runtime.getRuntime().exec(command) to make my system call. For Windows based Dos, i add "cmd /c" before I type in my system call. So for example make the system call "dir": String...
4
3547
by: uni | last post by:
I have the following Process o = Runtime.getRuntime.exec("java stuff",en); where en is -f CH-ResultsDB.. picked that up somewhere, anyways this code is run in a thread and i would like it to...
1
6923
by: maya2000 | last post by:
Hello, I searched this java newsgroup and tried all suggestions, but still failed. I keep watch a task manager, but cmd.exe never running. All I want to do is run and pop up the cmd.exe using...
0
1523
by: haritha | last post by:
HI Friends This blog is very useful to all sections of people. For Pc trouble shooting skills,Linux,Html Webdesigning,Free softwares,Networking skills,Gaming news and For Tips visit the blog:...
1
7534
Nepomuk
by: Nepomuk | last post by:
Hi! I'm trying to run an external Program with Process p = Runtime.getRuntime().exec("/bin/sh -c \"/bin/gzip -c /home/user/workspace/TarGz/pics.tar > pics.tar.gz\""); CleanStream cleanError =...
3
6965
by: Shayco | last post by:
hey, in my code i'm using Runtime.getRuntime().exec() in order to run a .bat file that calls another java program (they communicate with each other using RMI). when i call: Process process =...
0
7218
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7103
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7307
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.