Results 1 to 2 of 2
  1. #1
    LordSM is offline Member
    Join Date
    Aug 2007
    Rep Power

    Default Unable to execute command line command in java

    I am trying to run a command to add a group name. It is executed in command prompt of Windows 2003/2000
    Say this is the command I want to execute:
    net localgroup LordSM /add
    Here LordSM is the group name.

    I wrote the following code but it does not seem to work. Please suggest.

    String ABC=Value1TextField.getText();
    Value2Label.setText("Value is now " + ABC);

    // Above is to get value from a JText Field

    // Below is the code to run command for adding a group name to a System.
    String ExecutedCmd = "net localgroup " + ABC + "/add";
    Process p = Runtime.getRuntime().exec(ExecutedCmd);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

    // read the output from the command

    System.out.println("Here is the standard output of the command:\n");
    while ((s = stdInput.readLine()) != null)
    { System.out.println(s); }

    // read any errors from the attempted command

    System.out.println("Here is the standard error of the command (if any):\n");
    while ((s = stdError.readLine()) != null)
    { System.out.println(s); }

  2. #2
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Rep Power


    When Runtime.exec() won't - Java World
    Read this It will definitly help then dont forget to destroy the process , In the worst case If you are not successfull put the command in a batch and call the batch file , Your code will not work because you are not specifying the process itslelf , you have to specify it as cmd.exe or some batch file

Similar Threads

  1. Command line argument
    By denisatandi in forum New To Java
    Replies: 8
    Last Post: 10-16-2012, 11:37 PM
  2. Java command line
    By agouzoul in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 12:12 PM
  3. calling linux command line in java
    By fangzhong in forum New To Java
    Replies: 0
    Last Post: 02-03-2008, 04:24 PM
  4. Exporting from the command line
    By o1121 in forum Eclipse
    Replies: 1
    Last Post: 08-09-2007, 07:29 PM
  5. Java Command Line Arguments In Eclipse IDE
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 05-19-2007, 09:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts