• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

running OS commands in Java

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any java API to run OS(operating system) commands and return the output?
This is really urgent. Anybody can help me?
Thanks
 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the exec methods of java.lang.Runtime. I used it in one application to send mail via the mail program on a Unix server.
John
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look here to see an example.
 
nikita sri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the sample code and got an IOException. Can anybody figure out what's the problem with the code?
I am using windows millenium platform.
The code is as follows:
package javaUtilities;
java.io.*;
public class RunOSCommand {
public RunOSCommand() {
}
public static void main(String[] args) {
System.out.println("in the main");
try
{
Runtime r = Runtime.getRuntime();
String cmd = "dir";
Process p = r.exec(cmd);
BufferedReader reader = new BufferedReader(new InputStreamReader(new
DataInputStream( new BufferedInputStream(p.getInputStream()))));
String lineRead =null;
while( (lineRead = reader.readLine() ) != null)
{
System.out.println(lineRead);

}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
java.io.IOException: CreateProcess: dir error=0
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Win32Process.java:66)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:551)
at java.lang.Runtime.exec(Runtime.java:418)
at java.lang.Runtime.exec(Runtime.java:361)
at java.lang.Runtime.exec(Runtime.java:325)
at javaUtilities.RunOSCommand.main(RunOSCommand.java:26)
 
nikita sri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A small correction.
the import keyword should be included in the 2nd line.
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic