5 Replies Latest reply: Jul 22, 2009 4:59 AM by 843807 RSS

    Need to get batch file output in Java

    843807
      Hi all,

      I am trying to create a utility that will refer to serverStatus.bat on Websphere boxes and would get me back the server status in Java. I would need to get the status back, and depending on status, send email to the concerned person, so that he is aware that server is stopped.

      I am able to run the batch file from Java. I am trying to get the output in a text file. I am getting the value of commandResult as null. Below is my code snippet..

      BufferedWriter bw = null;
      String s = null;
      Runtime runtime = Runtime.getRuntime();
      Process p1 = runtime.exec("cmd /c start D:\\WebSphere6\\AppServer\\bin\\serverStatus.bat");
      File file1 = new File("D:\\Output.txt");
      bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file1)));
      BufferedInputStream buffer = new BufferedInputStream(p1.getInputStream());
      BufferedReader commandResult = new BufferedReader(new InputStreamReader(buffer));
      String line = null;
      StringBuffer sbuffer = new StringBuffer();
      try{
      while((line = commandResult.readLine())!=null){
      sbuffer.append(line);
      }
      s = buffer.toString();
      bw.write("commandResult is " + s);
      }catch(Exception e){
      e.printStackTrace();
      }

      Please help, if anyone has any idea.
        • 1. Re: Need to get batch file output in Java
          796365
          Getting results back from external processes can be tricky, as inadequate buffers may cause problems.

          Make sure that you're implementing the material from this article:
          http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
          • 2. Re: Need to get batch file output in Java
            843807
            Thanks for the reply and the details.

            I had tried the same piece of code as on Page 4 Listing 4.5....
            But then also I got the same error..i.e Output was returning as Null..

            Is there anything specific that needs to be added in the code?
            • 3. Re: Need to get batch file output in Java
              796365
              Possibly the server status information isn't being made available on standard out by the external process? If it isn't, then you can't get it in Java using this method.
              • 4. Re: Need to get batch file output in Java
                843807
                Thanks..
                I have setup an alternative way to get server status.
                I check process id on the folders, and if no process id is present it means that server is stopped.

                Thanks for your help!!! Let me know if you come across logic to read Serverstatus.bat in Java...
                • 5. Re: Need to get batch file output in Java
                  843807
                  hi
                  i have the solution. try this code it will work.

                  import java.io.*;
                  class test1{
                  public static void main(String args[])
                  {
                  BufferedWriter bw = null;
                  String s = null;
                  try
                  {

                  System.out.println("Running the batch script");
                  Runtime rt = Runtime.getRuntime();
                  Process p1 = rt.exec("d:\\test1.bat ");
                  FileWriter fw=new FileWriter("d:\\Output.txt");
                  BufferedInputStream buffer = new BufferedInputStream(p1.getInputStream());

                  BufferedReader commandResult = new BufferedReader(new InputStreamReader(buffer));
                  String line = "";
                  StringBuffer sbuffer = new StringBuffer();
                  try{
                  while((line = commandResult.readLine())!=null){
                  sbuffer.append(line+"\n");
                  s = sbuffer.toString();

                  }
                  fw.write("commandResult is " + s);

                  }catch(Exception e){
                  e.printStackTrace();
                  }
                  finally
                  {fw.close();}
                  System.out.println("Finished running the batch script");
                  }
                  catch(Exception e) {
                  System.out.println("Error creating the FileInfo panel: " +e);
                  e.printStackTrace();
                  }
                  }
                  }

                  Edited by: Raj_mtech on Jul 22, 2009 2:58 AM

                  Edited by: Raj_mtech on Jul 22, 2009 2:59 AM