Run commands using java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pjerald
    New Member
    • Oct 2007
    • 77

    Run commands using java

    Dear All

    I have written a simple program using thread. My aim is to run a command. Please lead me how to do ? I am just posting my start code here.

    [code=java]

    package com.pjerald.rob ot;

    import java.net.*;
    import java.io.*;

    public class RunCmd extends Thread
    {
    public static void main(String[] args)
    {
    //RunCmd rc = new RunCmd("ls -ltr");
    RunCmd rc = new RunCmd("mkdir -p test1/test2/test3");
    rc.start();
    }
    String cmd = null;
    RunCmd(String cmd) {
    this.cmd = cmd;
    }
    public void run() {
    if(cmd != null)
    {
    try{
    Thread t = new Thread(cmd);
    t.start();
    int count = 0;
    do
    {
    if(count == 100)
    {
    System.out.prin tln(" TIME OUT for Thread ::"+t.getName() );
    t.destroy();
    }
    t.sleep(500);
    System.out.prin tln("Thread ::"+t.getName() +" running");
    count = count+1;
    }
    while(t.isAlive ());
    }
    catch(Exception e)
    {
    e.printStackTra ce();
    }
    }
    }
    }


    [/code]
    Last edited by Ganon11; Aug 30 '08, 01:08 AM. Reason: Fixing [CODE] tags.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Your curly brackets are goofy and read the API documentation of the Thread
    class because line 22 doesn't do what you assume it does.

    kind regards,

    Jos

    Comment

    • pjerald
      New Member
      • Oct 2007
      • 77

      #3
      Thanks for the reply.

      Your curly brackets are goofy and read the API documentation of the Thread
      class because line 22 doesn't do what you assume it does.
      Sorry for code indentation.

      I read API documentation and understand that my constructor is equivalent to

      Thread(null, null, name);
      which is Thread(ThreadGr oup group, Runnable target, String name)

      I don know how to proceed with this. give an idea please.
      i will try and get back to you.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by pjerald
        Thanks for the reply.



        Sorry for code indentation.

        I read API documentation and understand that my constructor is equivalent to

        Thread(null, null, name);
        which is Thread(ThreadGr oup group, Runnable target, String name)

        I don know how to proceed with this. give an idea please.
        i will try and get back to you.
        Well, it's not just the indentation, the structure of your methods is just plain wrong
        (see the position of the curly brackets).

        If you want to run an external program have a look at the Runtime.exec() methods.
        Also read the API documentation for the Thread class: you can't destroy() a Thread
        safely and the method is therefore deprecated.

        kind regards,

        Jos

        Comment

        Working...