Results 1 to 4 of 4
  1. #1
    tej
    tej is offline Member
    Join Date
    Apr 2009
    Posts
    8
    Rep Power
    0

    Default how to get the values from command prompt

    hi all i have code which executes the .bat file from command prompt


    import java.io.*;
    class test{
    public static void main(String arg[]){
    try{
    String command = "cmd /C start C:/test.bat ";
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(command);

    }catch (IOException e) {
    e.printStackTrace();
    }
    }
    }


    here is the query
    how can i display the results which are executed by test.bat through java application.

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    take a look at the Process API doc.

    Java Code:
    String command = "cmd /c @echo Hello World!";
    ...
    Scanner scan = new Scanner(pr.getInputStream());
    ...
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    tej
    tej is offline Member
    Join Date
    Apr 2009
    Posts
    8
    Rep Power
    0

    Default

    here is mycode

    import java.io.*;
    class parsingapplication1
    {

    public static void main(String args[]) throws IOException
    {
    Process process = Runtime.getRuntime().exec( "cmd /C start C:/test.bat" );

    byte[] readBuffer = new byte[1024]; //created object for the byte class
    StringBuilder stderrBuffer= new StringBuilder(); //created object for StringBuilder

    InputStream errorIs = process.getErrorStream(); //to display errors
    int bytesRead = errorIs.read( readBuffer );
    while( bytesRead >= 0 )
    {
    stderrBuffer.append( new String( readBuffer, 0, bytesRead ) );
    bytesRead = errorIs.read( readBuffer );
    }
    // do something with stderrBuffer

    StringBuilder stdoutBuffer = new StringBuilder();
    InputStream inputIs = process.getInputStream();
    bytesRead = inputIs.read( readBuffer );
    while( bytesRead >= 0 )
    {
    stdoutBuffer .append( new String( readBuffer, 0, bytesRead ) );
    bytesRead = inputIs.read( readBuffer );
    }
    }

    please check it once

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you get any error message there in the above code. What you exactly going to do?

    And also, seems to me you've misses the last closing bracts in the class. ;)

Similar Threads

  1. Creating a Jar File Using Command Prompt
    By hitmen in forum New To Java
    Replies: 14
    Last Post: 08-27-2011, 12:05 PM
  2. Creating a Jar File Using Command Prompt
    By hitmen in forum New To Java
    Replies: 6
    Last Post: 03-18-2009, 04:37 PM
  3. Running applet from command prompt
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 1
    Last Post: 03-12-2009, 08:10 AM
  4. help me!!!! about command prompt..
    By kureikougaiji in forum New To Java
    Replies: 2
    Last Post: 11-13-2008, 06:15 PM
  5. Problem during executing Command Prompt
    By keshari in forum Advanced Java
    Replies: 4
    Last Post: 06-05-2008, 04:06 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
  •