Run a dos command from a Java class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Run a dos command from a Java class

  1. #1
    Alex Omar Pagan Ortiz Guest

    Run a dos command from a Java class



    Hi!

    I would like to know a simple routine or the java instructions to just
    execute a dos command, like "dir" or "java classname" from a java class.

    Regards,

    Alex

  2. #2
    Dhimant Patel Guest

    Re: Run a dos command from a Java class


    Did you tried Process class in java.lang?
    You might need to struggle with it though!

    -HTH
    DP.

    "Alex Omar Pagan Ortiz" <aortiz@dcti.com> wrote:
    >
    >
    >Hi!
    >
    > I would like to know a simple routine or the java instructions to just
    >execute a dos command, like "dir" or "java classname" from a java class.
    >
    >Regards,
    >
    >Alex



  3. #3
    Thomas Weinstein Guest

    Re: Run a dos command from a Java class


    Hi Alex,

    As in the prev. replay mentioned use:
    proc = Runtime.getRuntime().exec(String cmdline);

    But beware of a nice pitfall which costs me days of debugging.

    If you use exec() (at least with Windows NT) you are responsible
    to read the stdout and stderr stream of the process you've started. If you
    don not so your process will hang and appearantly never terminate. Following
    the code I used to solve
    this problem. Don't know if this is the most elegant or efficient method
    but it works for me.
    Just put the proc value you've got from exec() and put it into the following
    method:

    protected int showOutputAndWaitForProcEnd(Process proc) throws IOException
    {
    InputStream in = proc.getInputStream();
    InputStream err = proc.getErrorStream();
    BufferedReader readIn = new BufferedReader(
    new InputStreamReader(in));
    BufferedReader readErr = new BufferedReader(
    new InputStreamReader(err));
    int b;
    int exit = -1;

    boolean bReady = false;

    while (!bReady)
    {
    try
    {
    exit = proc.exitValue();
    bReady = true;
    }
    catch (IllegalThreadStateException isex)
    {
    }
    suckStreams(readIn,readErr);
    }

    readIn.close();
    readErr.close();

    return exit;
    }

    protected void suckStreams(BufferedReader readIn, BufferedReader readErr)
    {
    try
    {
    while (readIn.ready() || readErr.ready())
    {
    if (readIn.ready()) System.out.println(readIn.readLine());
    if (readErr.ready())
    System.err.println(readErr.readLine());
    }
    }
    catch (IOException ex)
    {
    System.err.println("suckStreams: " + ex);
    }
    }

    "Alex Omar Pagan Ortiz" <aortiz@dcti.com> wrote:
    >
    >
    >Hi!
    >
    > I would like to know a simple routine or the java instructions to just
    >execute a dos command, like "dir" or "java classname" from a java class.
    >
    >Regards,
    >
    >Alex



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center