The Artima Developer Community
Sponsored Link

Java Answers Forum
EXECUTING SYSTEM COMMANDS THRU JAVA

4 replies on 1 page. Most recent reply: Feb 9, 2004 1:20 AM by Viswanatha Basavalingappa

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Sunitha C S

Posts: 20
Nickname: sunics
Registered: Dec, 2003

EXECUTING SYSTEM COMMANDS THRU JAVA Posted: Feb 5, 2004 2:37 AM
Reply to this message Reply
Advertisement
Hi all,

i am in need of executing the operating system commands eg: ping, dir etc, in java programs. how can i do that?
please help at the earliest.

Sunitha


mahashankar sarangapani

Posts: 7
Nickname: greatmaha
Registered: Feb, 2004

Re: EXECUTING SYSTEM COMMANDS THRU JAVA Posted: Feb 5, 2004 10:16 PM
Reply to this message Reply
hi sunitha!

you can use these following command to resolve your problem
Runtime rt = Runtime.getRuntime();
rt.exec(cmd);
cmd is the command with full path to execute.

Sunitha C S

Posts: 20
Nickname: sunics
Registered: Dec, 2003

Re: EXECUTING SYSTEM COMMANDS THRU JAVA Posted: Feb 6, 2004 1:32 AM
Reply to this message Reply
Hello,
thanks for the method. but how can the output of the command executed will be printed on the screen?
Sunitha

David Ramsey

Posts: 34
Nickname: dlramsey
Registered: Apr, 2002

Re: EXECUTING SYSTEM COMMANDS THRU JAVA Posted: Feb 6, 2004 9:17 AM
Reply to this message Reply
Runtime.exec(cmd) will return a java.lang.Process object. Go read up on the Process class in the SDK Javadocs. You can get an input and output stream from the process and then manipulate those streams.

Viswanatha Basavalingappa

Posts: 84
Nickname: viswagb
Registered: Nov, 2003

Re: EXECUTING SYSTEM COMMANDS THRU JAVA Posted: Feb 9, 2004 1:20 AM
Reply to this message Reply
YES, It is possible
here is the code for the same....

import java.io.*;
class JavaTest
{
public static void main(String[] args)
{
try
{ Runtime rt= Runtime.getRuntime();
Process p = rt.exec("Dir"); // here u can do what ever dommand line execution...
//OutputStream op =p.getOutputStream();
InputStream ip = p.getInputStream();
}
catch(Exception IOException )
{

}
}
}

I hope this helps you....if you need more info let me know

Viswa
IBM - Bangalore
http://viswa.itgo.com
------------------

Flat View: This topic has 4 replies on 1 page
Topic: JVM v. JRE Previous Topic   Next Topic Topic: How to make a JAR file?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use