0

I have looked for solution in stackoverflow and google for this problem however I have not found anything solid that can help me to solve it.

I'm trying to execute a bat file with powershell command using java and then storing the response in StringBuilder but the application just hangs. Here is what I have done

------BATCH FILE------------

Powershell /C "Get-WMIObject -class Win32_ComputerSystem | select username"

------JAVA FILE-------------

import java.io.InputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.BufferedWriter;
import java.io.FileWriter;

import java.util.*;

//import org.apache.commons.exec.CommandLine;
//import org.apache.commons.exec.DefaultExecutor;

public class Test
{
    public static void main(String[] args)
    {
        try {
            String command = "cmd /C .\\scripts\\Get-Username.bat";

            System.out.println(executeCommand(command).toString());

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

    public static StringBuilder executeCommand(String command)
    {
        String line = null;
        StringBuilder responseBuffer = new StringBuilder();
        try {

            Process process = Runtime.getRuntime().exec(command);

            BufferedReader inStreamReader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));

            while((line = inStreamReader.readLine()) != null){
                //System.out.println(line);
                responseBuffer.append(line + "\n");
            }


            process.getOutputStream().close();

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

        return responseBuffer;
    }
}

Thank you in advance

  • Where does it hang? Have you traced through it in an IDE debugger? – Jim Garrison Aug 31 '14 at 07:49
  • @JimGarrison I don't use an IDE. I use ant and text editor as my build tools. Because my laptop is ancient, it does not have much memory to run IDE properly. –  Aug 31 '14 at 07:54
  • 1
    @JimGarrson Correct me if i'm wrong, far as I can understand, it seems not to go past "while" statement –  Aug 31 '14 at 07:56
  • Similar question: http://stackoverflow.com/q/11573457 – Luke Woodward Aug 31 '14 at 08:24

2 Answers2

1

You are trying to execute a batch file from a network share. The operation probably hangs due to networking issues, even when executed from the command line. A variant hypothesis is that the command expects user input, such as credentials for the network share. There may be conditions under which this behavior is exhibited only when run through Java.

To gain more insight into the happenings I suggest printing out each character you manage to read (followed by System.out.flush()), as well as merging the standard output and error into a single stream observed within Java. Refer to the documentation of ProcessBuilder to find out how to achieve that.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

the problem is that you read whole lines from the stream...

try reading int values, because it's not guaranteed to return a whole line...

int sign = 0;
while(sign != -1){
    sign = process.getInputStream().read();
    char c = (char)sign;
    System.out.print(""+c);
}

i have this answer from http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#start%28%29

Martin Frank
  • 3,445
  • 1
  • 27
  • 47
  • 1
    Sorry this did not work and it still hangs. anymore ideas, you would like to share –  Aug 31 '14 at 08:11
  • have you used that read solely or combined with your previous code? – Martin Frank Aug 31 '14 at 08:20
  • To clarify, what Martin asked you is whether you *removed the call to `readLine()`* from your code because that particular call is hypothesised here as causing the stall. You should also remove `new BufferedReader(new InputStreamReader(...))`. – Marko Topolnik Aug 31 '14 at 09:03
  • Yes, I have removed readline() however i did not remove BufferedReader but still it hangs when I do. here is the new iteration [link]http://pastie.org/9516710 –  Aug 31 '14 at 09:23
  • Your new finding strengthens my hypothesis, which is that your blocking is either inherent in the behavior of your command (the command blocks on a resource other than stdin/stdout), or, possibly, the command expects some user input (perhaps credentials for the network share). – Marko Topolnik Aug 31 '14 at 09:33
  • For the sake of time, I decided to work around it by outputting the result to a log file using an powershell Out-File cmdlet and then reading the log file. –  Aug 31 '14 at 11:41