472,345 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 software developers and data experts.

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 40236
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
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...
1
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...
1
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
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...
4
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...
1
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...
0
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...
1
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...
3
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.