user3860307
user3860307

Reputation:

How to stop java from hanging after exec runtime command

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

Upvotes: 0

Views: 545

Answers (2)

Marko Topolnik
Marko Topolnik

Reputation: 200168

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.

Upvotes: 1

Martin Frank
Martin Frank

Reputation: 3454

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

Upvotes: 0

Related Questions