473,796 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System call to different OS using Runtime Class in Java

Hi,

I use Runtime.getRunt ime().exec(comm and) 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.getRunt ime().exec(comm and);

What is the equivalenr of cmd /c in Unix .

Thank you in advance.
Ayesha
Jul 17 '05 #1
5 14413
On 8 Jan 2004 10:26:24 -0800, ay****@ou.edu (Ayesha Ahsan) wrote:
Hi,

I use Runtime.getRunt ime().exec(comm and) 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.getRun time().exec(com mand);

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.getRunt ime().exec(comm and);
--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
Jul 17 '05 #2
["Followup-To:" header set to comp.unix.quest ions.]
On 8 Jan 2004 10:26:24 -0800, Ayesha Ahsan
<ay****@ou.ed u> wrote:
Hi,

I use Runtime.getRunt ime().exec(comm and) 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.getRunt ime().exec(comm and);

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
ay****@ou.edu (Ayesha Ahsan) wrote in message news:<13******* *************** ****@posting.go ogle.com>...
What is the equivalenr of cmd /c in Unix .

sh -c
Jul 17 '05 #4

"Ayesha Ahsan" <ay****@ou.ed u> wrote in message
news:13******** *************** ***@posting.goo gle.com...
Hi,

I use Runtime.getRunt ime().exec(comm and) 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.getRunt ime().exec(comm and);

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.FileInp utStream;
import java.io.InputSt reamReader;
import java.io.Buffere dReader;
import java.io.IOExcep tion;

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

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

String commandOutput = null;

try
{
Process p = Runtime.getRunt ime().exec(cmdS tring);

BufferedReader pOutput =
new BufferedReader(
new InputStreamRead er(p.getInputSt ream()));

String line;
StringBuffer tmpCommandOutpu t = new StringBuffer();

try
{
while ((line = pOutput.readLin e()) != null)
tmpCommandOutpu t.append(line). append("\n");

commandOutput = tmpCommandOutpu t.toString();
}

catch (IOException e) {}

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

catch (IOException e) {}
catch (InterruptedExc eption e) {}

return commandOutput;
}
}
Jul 17 '05 #5
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4965
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
12
2434
by: R | last post by:
Hello everybody. I'm writing my own Content System in PHP5. I've written so far main classes for handling DB connections, XML, XForms and Sessions. But I've got problem with one thing - it's not even relative with implementation - I'm looking for a smart solution My present system works like this:
2
7438
by: Ashwini Kumar | last post by:
Hi, I want to know if there is a way to get system level information using Java. I am mostly interested in things like Ethernet ID and serial number of Hard disk. Some of the values when you type "ipconfig /all" on windows. I know we can do all this using JNI with the main routines written in C. But is there a way to do it using Java alone?. I have not seen any API to help me get such deep system level information.
1
2032
by: eBob.com | last post by:
Well I am now on page 84 of Bruce Eckel's "Thinking in Java". (... having made significant progress since the help you guys gave me in response to my last question about page 34.) Here he presents a simple program which uses two classes, System and Runtime. And he suggests using packages.html to find those two classes. He says "If you look at the packages.html file, you'll see a list of all the different class libraries that come with...
5
12250
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the event handlers may take longer than the interval for either of the timers, so it's possible for multiple events to fire "simultaneously" and for events to queue up. I'm attempting to get the timers to sync on some reference type object, or use...
3
4035
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user selects? thanks...
4
2648
by: Brandon McCombs | last post by:
Hello, From my understanding, DirectoryEntry is used to connect to Active Directory. Although this makes no sense whatsoever I accept it. The problem is how am I supposed to test whether a connection is active or not if I can create a DirectoryEntry with whatever data I want since the data really isn't verified until I try to use it? I am not able to determine whether the connection is really going to be available until the Try {}...
2
4502
by: =?Utf-8?B?TmF0aGFuIFdpZWdtYW4=?= | last post by:
Hi, I am wondering why the .NET Framework is quite different from Win32 API when it comes to displaying system modal message boxes. Consider the four following types of system modal message boxes (please see associated source code below): 1) Win32 API with context ("btnWin32WithContext_Click") 2) .NET Framework with context ("btnFrameworkWithContext_Click") 3) Win32 API withOUT context ("btnWin32WithOUTContext_Click")
7
10600
by: WTH | last post by:
I am now aware (I am primarily a C++ developer) that in C# if you reference the same interface from the same file in two different projects the types are actually incompatible. I found this out because I have written a generic plugin system for my current and future C# needs. I defined a base plugin system interface named IPlugin (original, I know...) which contains some basic plugin infomration all plugins of this system must expose...
0
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.