• 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:

exec() in win2000

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am running windows 2000, and i have having some problems running this code. It work when for exec("ping"), or exec("ftp") but trying to run exec("dir") or exec("set") it breaks. Can anyone help??? I am guessing that i need to give java more rights to run system commands or something??? Thank you for any help...
import 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 = ("set");
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();
}
}
}
// --------------------------------------------------------
H:\java>java RunOSCommand
in the main
java.io.IOException: CreateProcess: set error=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Win32Process.java:64)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:300)
at java.lang.Runtime.exec(Runtime.java:247)
at RunOSCommand.main(RunOSCommand.java, Compiled Code)
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Use
Runtimert=Runtime.getRuntime ();
Process p = rt.exec ("cmd /c dir);
This is used when you invoke a dos command.
Bye
Xavier
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem you are having is that dir and set are commands within DOS. You will have to exec cmd.exe or command.com and then send your DOS commands to the resulting process.
 
Happily living in the valley of the dried frogs with a few tiny ads.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic