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

System call to different OS using Runtime Class in Java

P: n/a
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 command="cmd /c dir";
Runtime.getRuntime().exec(command);

What is the equivalenr of cmd /c in Unix .

Thank you in advance.
Ayesha
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 8 Jan 2004 10:26:24 -0800, ay****@ou.edu (Ayesha Ahsan) wrote:
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 command="cmd /c dir";
Runtime.getRuntime().exec(command);

What is the equivalenr of cmd /c in Unix .


While I'm unfamiliar with Java's implementation of the system() method, I assume
that it is similar to the standard Unix system() call. /That/ call does not need
'helper' applications to launch the required application, but launches it
directly using one of the execv() calls.

So, the Unix equivalent of your MSWindows "dir" system call would be

String command="ls";
Runtime.getRuntime().exec(command);
--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
Jul 17 '05 #2

P: n/a
["Followup-To:" header set to comp.unix.questions.]
On 8 Jan 2004 10:26:24 -0800, Ayesha Ahsan
<ay****@ou.edu> wrote:
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 command="cmd /c dir";
Runtime.getRuntime().exec(command);

What is the equivalenr of cmd /c in Unix .

sh -c would be the closest equivalent, but the syntax of sh is quite
different and "dir" is not a builtin command.
--
Absurd Procrustean Egghead Cornstarch Variant Bill Marcum
Jul 17 '05 #3

P: n/a
ay****@ou.edu (Ayesha Ahsan) wrote in message news:<13**************************@posting.google. com>...
What is the equivalenr of cmd /c in Unix .

sh -c
Jul 17 '05 #4

P: n/a

"Ayesha Ahsan" <ay****@ou.edu> wrote in message
news:13**************************@posting.google.c om...
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 command="cmd /c dir";
Runtime.getRuntime().exec(command);

What is the equivalent of cmd /c in Unix .


Since you cross-posted to UNIX NG's I'm sure you received fairly in-depth
responses to your query. Still, it might be worth mentioning that *NIX /
Linux systems actually have several, fairly standard, command interpreters
[or 'shells', as they are commonly called] rather than the single one found
on Windows-family systems.

The default shell, which is, on Linux systems in particular, a Bourne Shell
derivative like 'bash', is commonly aliased as 'sh', and, as has been
mentioned, is invoked via:

sh -c

From a Java usage standpoint it is worth bearing in mind that:

* Invoking a shell creates another process, hence any output will
not appear on your Java console. It must, instead, be redirected
to external files, or be captured by your program [see below]

* Passing arguments to shells can be quite error prone, particularly
if parts of command strings are not properly 'escaped'. Also,
it seems safest to pass arguments to *NIX / Linux shells via
a 'String[]' rather than a single 'String'

I hope this helps.

Anthony Borla

// ===================================

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

public class ShellCommands
{
public static void main(String[] args)
{
System.out.print(executeShell("sh", "-c", "ls", "*.xxx", retCode)
+ " : " + retCode[0]);
}

private static String executeShell(String shell, String shellOpts,
String cmd, String cmdArgs, int[] retCode)
{
String[] cmdString = { shell, shellOpts, cmd, cmdArgs };

String commandOutput = null;

try
{
Process p = Runtime.getRuntime().exec(cmdString);

BufferedReader pOutput =
new BufferedReader(
new InputStreamReader(p.getInputStream()));

String line;
StringBuffer tmpCommandOutput = new StringBuffer();

try
{
while ((line = pOutput.readLine()) != null)
tmpCommandOutput.append(line).append("\n");

commandOutput = tmpCommandOutput.toString();
}

catch (IOException e) {}

p.waitFor(); retCode[0] = p.exitValue(); pOutput.close();
}

catch (IOException e) {}
catch (InterruptedException e) {}

return commandOutput;
}
}
Jul 17 '05 #5

P: n/a
> For Windows based Dos, i add "cmd /c" before I type in my system
call.


don't use "cmd". Instead, read the value of the COMSPEC environment
variable
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.